Copy a range of adjacent Columns from Sheet1 to Sheet2 - copy

I am trying to copy adjacent columns U:W from sheet1 to sheet2 with the following code and don’t work...what is wrong????
Dim rngUW As Range
Set rngUW = Worksheets("Sheet1").Range(Range("U2:W2"), Range("U1:W1").End(xlDown))
Worksheets("Sheet2").Range("C1").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value

Related

VBA Autofilter subset contains extra row up top

The macro attempts to filter Sheet "Temp" for one Criteria at a time (PE, AR, DC,FI), and copy column 5 that contains non-duplicate data into another sheet "Detail". Please help me understand two issues. (1) The macro does correct filtering for each of the 4 criteria. However, the filtered list for each of the criteria always contains the first item from the filtered list of the very first criteria "PE". I.e. the filtered list for criteria 2, "AR", contains all items in AR, but starts with the first item in "PE". There's a header row, but it doesn't seem to make a difference. How can I get rid of that first item in all cases except when filtering for "PE" (where it belongs)? (2) I would like to be able to count and store the number of visible rows for each filtered list. I would like to be able to paste each filtered list into another spreadsheet ("Detail"), starting in cell A4. Each consecutive list should start two rows below the list that was just pasted. For example, if the first list contains 16 items, then the next list should start in cell A22 (A4+16+2). For some reason, copiedrows (used to remember number of rows in a filtered list) is correct the first time around (=16), but not the second time (=1?). It looks like q's 1 & 2 are related. Perhaps, if I figure out #1, I can do something about #2. I reviewed exiting posts on Autofiltering, but still feel a bit stuck here. Really appreciate your help!
Sub FilterCategories()
Dim LastRow As Long
Dim startpos As Integer
Dim k As Integer
Dim copiedrows(1 To 4) As Long
Dim AG(1 To 4) As String
Dim rng As Range
AG(1) = "PE"
AG(2) = "AR"
AG(3) = "DC"
AG(4) = "FI"
'Autofilter based on each AG and copy over to 'Detail'. Create temporary
sheet for filtering.
startpos = 4
For k = LBound(AG) To UBound(AG)
Application.DisplayAlerts = False
On Error Resume Next
Sheets("Temp").Delete
Sheets("Lookup").AutoFilterMode = False
Sheets("Lookup").Copy After:=Sheets("Lookup")
ActiveSheet.Name = "Temp"
With Sheets("Temp")
AutoFilterMode = False
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
With .Range("A2:E" & LastRow)
.AutoFilter Field:=4, Criteria1:=AG(k)
.RemoveDuplicates Columns:=5
.Columns(5).SpecialCells(xlCellTypeVisible).Copy
Destination:=Sheets("Detail").Range("A" & startpos)
copiedrows(k) = .SpecialCells(xlCellTypeVisible).Rows.Count
Debug.Print copiedrows(k)
startpos = copiedrows(k) + 6
Debug.Print startpos
End With
End With
Next
End Sub

Copy data from one sheet, add current date to each new row, and paste

I've done some reading but my limited knowledge on scripts is making things difficult. I want to:
Copy a variable number of rows data range, known colums, from one sheet titled 'Download'
Paste that data in a new sheet titled 'Trade History' from Column B
In the new sheet, add today's date formatted (DD/MM/YYYY) in a new column A for each record copied
The data in worksheet 'Download' uses IMPORTHTML
The data copied from Download to store a historical record needs a date in Column A
I've managed to get 1 and 2 working, but can't work out the 3rd. See current script below.
function recordHistory() {
var ss = SpreadsheetApp.getActive(),
sheet = ss.getSheetByName('Trade_History');
var source = sheet.getRange("a2:E2000");
ss.getSheetByName('Download').getRange('A2:E5000').copyTo(sheet.getRange(sheet.getLastRow()+1, 2))
}
You need to use Utilities.formatDate() to format today's date to DD/MM/YYYY.
Because you're copying one set of values, and then next to it (in column A), pasting another, I altered your code a bit as well.
function recordHistory() {
var ss = SpreadsheetApp.getActive(),
destinationSheet = ss.getSheetByName('Trade_History');
var sourceData = ss.getSheetByName('Download').getDataRange().getValues();
for (var i=0; i<sourceData.length; i++) {
var row = sourceData[i];
var today = Utilities.formatDate(new Date(), 'GMT+10', 'dd/MM/yyyy'); // AEST is GMT+10
row.unshift(today); // Places data at the beginning of the row array
}
destinationSheet.getRange(destinationSheet.getLastRow()+1, // Append to existing data
1, // Start at Column A
sourceData.length, // Number of new rows to be added (determined from source data)
sourceData[0].length // Number of new columns to be added (determined from source data)
).setValues(sourceData); // Printe the values
}
Start by getting the values of the source data. This returns an array that can be looped through to add today's date. Once the date has been added to all of the source data, determine the range boundaries for where it will be printed. Rather than simply selecting the start cell as could be done with the copyTo() method, the full dimensions now have to be defined. Finally, print the values to the defined range.

How to import range in google spreadsheet ignoring the Error "overwrite data"?

In above picture I have the following formula importrange("spreadsheet_id","Sheet!A2:P1010") in cell A2767
I have some data below now is there any way to ignore that error and let it fetch and overwrite?
Importrange needs blank cells to hold the size of the content you are importing. If the example you gave is in column A then "A67:P1077" need to be cleared. You can have data in the sheet after that for those rows and columns. You can force that to happen with
function onEdit(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("importtosheetname");// replace with your sheet name
var values = sheet.getRange("A68:P1077").getValues(); //note that the formula cell is not deleted assuned to be A67
if (values[0][0] == "#REF!")
sheet.getRange("A67:P1077").clearContent();
}

Matlab: Concatenating information from multiple sheets

I have an excel file 'Bool1.xlsx' with sheets named: 'Sheets1', 'Sheets2'.
Sheet1 has information of following sort:
Month Val1 Val2
1-Feb-14 (1.17) (13.18)
1-Apr-14 (1.12) (195.39)
Total (3.08) (180,501.38)
Sheet2 has infomation of sort:
Month Val1 Val2
1-Feb-14 (3.17) (17.18)
1-Apr-14 (2.12) (15.39)
Total (3) (10,51.38)
I want to create a Sheet 3 which pulls information from Sheet 1 and Sheet 2 and adds a column at the end like:
Month Val1 Val2 Source
1-Feb-14 (1.17) (013.18) Sheet1
1-Apr-14 (1.12) (195.39) Sheet1
1-Feb-14 (3.17) (17.18) Sheet2
1-Apr-14 (2.12) (15.39) Sheet2
If the sheets only that numerical values I don't find it hard to do it elegantly but with mixture of numerical and text values I am not able to do it in a clean manner. Any ideas guys ?
Edit:
I must achieve this in Matlab as this is part of another bigger piece of Matlab code that I am running.
Thanks
Have you tried xlsread?
So something like:
[~,~,Sheet1] = xlsread(myXLSfile, 'Sheet1');
[~,~,Sheet2] = xlsread(myXLSfile, 'Sheet2');
Sheet1(:, end+1) = {'Sheet1'};
Sheet2(:, end+1) = {'Sheet2'};
[Sheet1; Sheet2]

set Datagirdviewcombobox column value in Vb.net

I have datagrid with two datagridviewcombo column, one column is dynamic fill and one has fixed hardcoded values.
The problem is I can't set the value of dynamic GridViewComboBox, when I try to set it generates continues errors.
System.FormateException: DataGridViewComboBoxCell Value is not valid.
My code to load the grid is
Dim dt As DataTable
dt = GetDataTable("Select valuecol,displayCol From mytable") 'GetDataTable gives me datatable
cmbAntibiotics.DataSource = dt
cmbAntibiotics.DisplayMember = "Antibiotics"
cmbAntibiotics.ValueMember = "AntibioticsID"
Dim Index As Integer
Dim dgr As DataGridViewRow
For Each dr As DataRow In dtFromDB.Rows 'This datatable is filled with database
Index = dtFromDB.Rows.Count - 1
GRDAntimicrobials.Rows.Add()
GRDAntimicrobials.Rows(Index).Cells("cmbAntibiotics").Value = dr("AntibioticsID").ToString 'At this point it shows value (1,2,3) rather then showing its display members
GRDAntimicrobials.Rows(Index).Cells("AntibioticsStatus").Value = dr("AntibioticsStatus").ToString
Next
Pls help with me
It seems like you're trying to assign the value to whatever there is on the cell rather than instantiate the object that resides in the cell and then assign its value. I would try something like this:
Dim vComboBoxColumn As DropDownList = DirectCast(GRDAntimicrobials.Rows(index).Cells("cmbAntibiotics"))
vComboBoxColumn.Value = dr("AntibioticsStatus").ToString