Defining dynamic range - vba

1) I have a sheet named C.A.
I want to copy the range starting from B$3 to the last cell (which is of column H)
2) Then paste it to another sheet called 2017
To 2 cells below the (last cell of column B containing data)
I did the second one, but, cant define the first one.
Dim lastRow As String
lastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row + 2
Range("B" & lastRow).Select
Selection.PasteSpecial
End Sub

Try this:
Dim ws_ca As Worksheet
Dim ws_17 As Worksheet
Dim nr_2017 As Long
Dim nr_ca As Long
Set ws_ca = ThisWorkbook.Worksheets("C.A.")
Set ws_17 = ThisWorkbook.Worksheets("2017")
nr_ca = ws_ca.Cells(Rows.Count, 8).End(xlUp).Row
nr_2017 = ws_17.Cells(Rows.Count, 2).End(xlUp).Row
ws_17.Range("B" & nr_2017 + 3 & ":H" & nr_2017 + nr_ca).Value = ws_ca.Range("B3:H" & nr_ca).Value

Related

How to select last three row automatically on a give range in EXCEL VBA

I created a code to select and paste data from one sheet to another. But this code is always selecting last three values in row.
I need to select the data based on given range. Eg C5:C15 not for the entire c column. Help me
Private Sub CommandButton1_Click()
Dim LastRow1, LastRow2, LastRow3 As Long
Dim Last3Rows1, Last3Rows2, Last3Rows3 As Range
LastRow3 = Sheets("AVG-PO").Range("C" & Rows.Count).End(xlUp).Row
LastRow1 = Sheets("AVG-PO").Range("A" & LastRow3).End(xlUp).Row
LastRow2 = Sheets("AVG-PO").Range("B" & LastRow3).End(xlUp).Row
Set Last3Rows3 = Sheets("AVG-PO").Range("C" & LastRow3).Offset(-2, 0).Resize(3, 1)
Set Last3Rows1 = Sheets("AVG-PO").Range("A" & LastRow3).Offset(-2, 0).Resize(3, 1)
Set Last3Rows2 = Sheets("AVG-PO").Range("B" & LastRow3).Offset(-2, 0).Resize(3, 1)
Last3Rows1.Select
Selection.Copy Sheets("Data").Range("A30")
Last3Rows2.Select
Selection.Copy Sheets("Data").Range("B30")
Last3Rows3.Select
Selection.Copy Sheets("Data").Range("C30")
End Sub
Not sure i quite understand what range you want but you might be able to use this.
Sub test()
Dim NextFree As Long
Dim TableStartRange As Long
For i = 1 To 100
Select Case Range("A" & i).Value
Case "Table1"
TableStartRange = i + 1 'finds table1 in A2 and then +1 before the actual table start in column C
End Select
NextFree = Range("C" & TableStartRange & ":C" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row ' returns the value 14 since its the first free row in column C
End Sub

How to copy a range of cells to another column in VBA?

Working Environment: Excel 2013
Target: Copy C1:C9 to B11:B19. D1:D9 to B21:B29. E1:E9 to B31:B39.....
After copying all the range to column B, copy A1:A9 to A11:A19(A21:A29....)
My idea is that:
1. select a range by using something like
range.end()
because in some of my sheets, there are only 4 test steps. so I need a syntax which can self inspect the used cells in a column.
do a range copy to column B.
leave 1 row in between considering about the page layout.
My piece of code is:
Worksheets("Master").Columns(3).UsedRange.Copy
Worksheets("Master").Range("B11").PasteSpecial
but seems like the Columns(i).UsedRange.Copy doesn't work. the pastespecial works.
My question is:
How to select the used range in columns? The number of columns are not fixed which means some of the sheets have 40 columns, but some of the other have maybe 30.
Thanks!
I attached one screenshot of the sheet for your reference.
Assuming you do not have more data in the columns to be copied, this should work
Sub copyToOneColumn()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Master")
Dim startCol As Integer
startCol = 3
Dim endCol As Integer
endCol = 10
Dim startRange As Range
Dim ra As Range
For i = startCol To endCol
Set startRange = ws.Range("A1").Offset(0, i - 1)
Set ra = ws.Range(startRange, ws.Cells(Rows.Count, startRange.Column).End(xlUp))
ra.Copy Destination:=ws.Range("B" & Rows.Count).End(xlUp).Offset(2, 0)
Next i
End Sub
You can do a copy (not technically a copy as it doesn't use the clipboard) directly like so:
Range("B1").Resize(Range("A1:A" & range("A" & Rows.Count).End(xlUp).Row).Rows.Count,1) = Range("A1:A" & range("A" & Rows.Count).End(xlUp).Row).Value
Effectively you are looking at B1 then resizing that to a range to be the number of columns in column A that are used with this: Range("A1:A" & range("A" & Rows.Count).End(xlUp).Row).Rows.Count
Then you are making this new range in column B = to the values of the same range in column A.
Note, this can be shortened if you are always starting at row 1 but the code I have given you will suffice if you start at a different row.
You may try something like this...
Sub CopyData()
Dim wsMaster As Worksheet
Dim lr As Long, lc As Long, r As Long, c As Long
Application.ScreenUpdating = False
Set wsMaster = Sheets("Master")
lr = wsMaster.Cells(Rows.Count, 1).End(xlUp).Row
lc = wsMaster.Cells(1, Columns.Count).End(xlToLeft).Column
r = lr + 2
If lr <= 9 Then
For c = 3 To lc
wsMaster.Range(wsMaster.Cells(1, c), wsMaster.Cells(lr, c)).Copy wsMaster.Range("B" & r)
wsMaster.Range("A1:A" & lr).Copy wsMaster.Range("A" & r)
r = wsMaster.Cells(Rows.Count, 2).End(xlUp).Row + 2
Next c
End If
Application.ScreenUpdating = True
End Sub

Excel VBA loop through visible filtered rows

I have a excel table with a autofilter.
In the filtered table i only have few rows filtered.
My objective is icterate all visible rows to colect data to copy to anothe sheet.
I want a way to collect a variable with the the fisrt visible row number.
my draft code is:
Dim cnp As String
Dim nome As String
Dim filter_rng As Range
Dim rw As Range
Dim last_row As Long 'last visible data row
Dim dest_row As Long 'row to paste the colected data
Set filter_rng = Range("A5:Y" & last_row).Rows.SpecialCells(xlCellTypeVisible)
'collect data
For Each rw In filter_rng.SpecialCells(xlCellTypeVisible)
workshett(1).Activate
cnp = Range("a" & rw).Value
nome = Range("b" & rw).Value
'copy data to another worksheet first data line is cell A2
Worksheet(2).Activate
Range("A" & dest_row + 1).Value = cnp
Range("b" & dest_row + 1).Value = nome
Next rw
Your code contains several errors and you provide little additional information to allow us to help you, but to put in an attempt.
Please see below code and compare to yours, the below code is closest to what you are trying to do and is tested and working.
Dim cnp As String
Dim nome As String
Dim filter_rng As Range
Dim rw As Range
Dim last_row As Long 'last visible data row
Dim dest_row As Long 'row to paste the colected data
last_row = 200
dest_row = 1
Set filter_rng = Sheets(1).Range("A5:Y" & last_row)
'collect data
For Each rw In filter_rng.SpecialCells(xlCellTypeVisible)
'Worksheets(1).Activate
cnp = Sheets(1).Range("A" & rw.Row).Value
nome = Sheets(1).Range("B" & rw.Row).Value
'copy data to another worksheet first data line is cell A2
'Worksheets(2).Activate
Sheets(2).Range("A" & dest_row + 1).Value = cnp
Sheets(2).Range("B" & dest_row + 1).Value = nome
Next rw

Copying Multiple columns in Excel-Vba

Hi I am trying to copy multiple columns from one workbook to another, and below is the code how I copied one and need help in making the code more optimized as I don't want to write same code for all the columns. below is the code.
Sub Copymc()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("H:\testing\demo\test2.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")
Dim LastRow As Long
Dim NextRow As Long
' determine where the data ends on Column B Sheet1
x.Worksheets("Sheet1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Select
LastRow = ActiveCell.Row
' copy the data from Column B in Sheet 1
Range("A2:A" & LastRow).Copy
' Determine where to add the new data in Column C Sheet 2
y.Worksheets("Sheet1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Offset(1, 0).Select
NextRow = ActiveCell.Row
' paste the data to Column C Sheet 2
y.Worksheets("Sheet1").Range("A" & NextRow).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select
End Sub
I tried to put all columns in range statement but problem I found was how to paste? How can I do it for multiple columns without repeating the code? Thanks in advance.
Let's say you want to copy columns A-D:
Sub Copymc()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("H:\testing\demo\test2.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")
Dim LastRow As Long
Dim NextRow As Long
' determine where the data ends on Column B Sheet1
x.Worksheets("Sheet1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Select
LastRow = ActiveCell.Row
' copy the data from Column B in Sheet 1
Range("A2:D" & LastRow).Copy y.worksheets("Sheet1").range("a65536").end(xlup).offset(1,0)
' Determine where to add the new data in Column C Sheet 2
'y.Worksheets("Sheet1").Activate
'Range("A65536").Select
'ActiveCell.End(xlUp).Offset(1, 0).Select
'NextRow = ActiveCell.Row
' paste the data to Column C Sheet 2
'y.Worksheets("Sheet1").Range("A" & NextRow).Select
'ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select
End Sub
I try to avoid the copy and paste functions as much as possible. To get around this I would loop through all of the values in the column and move them to your other workbook as such:
Sub test()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("H:\testing\demo\test2.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")
Dim LastRow As Long
LastRow = x.Sheets("Sheet1").Range("A65536").End(xlUp).Row
For i = 1 To LastRow
CopyVal = x.Sheets("Sheet1").Range("A1").Offset(i, 0).Value
CopyVal2 = x.Sheets("Sheet1").Range("A1").Offset(i, 1).Value
CopyVal3 = x.Sheets("Sheet1").Range("A1").Offset(i, 2).Value
CopyVal4 = x.Sheets("Sheet1").Range("A1").Offset(i, 3).Value
y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 3).Value = CopyVal4
y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 2).Value = CopyVal3
y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 1).Value = CopyVal2
y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 0).Value = CopyVal
Next
End Sub

how to break large excel sheet into multiple sheets

I have large amount of excel sheet where 16k records i want to break it into multiple sheets like in 2000 records in 1 excel sheets and if any think left so it should be add in next sheet please help me out of this.
Thanks in advance so much
Vijay Bhatt
I do not know how much you know about VBA but you can use following codes to achieve your goal. I will post 2 options. First macro will delete the lines which copied from the source sheet, second macro will keep source sheet values. All you need to do is update the code with the name of your source worksheet and how many lines you want in each generated sheet.
Update Items
Set CWS = Sheets("Sheet2") 'Source Worksheet name
LineNo = 5 ' Number of lines in each sheet
Macro 1: Will delete copied rows from source sheet.
Dim CWS As Worksheet
Dim LastRow As Long
Dim S_No As Long
Dim LineNo As Long
Set CWS = Sheets("Sheet2") 'Source Worksheet name
LastRow = Range("A" & Rows.Count).End(xlUp).Row
LineNo = 5 ' Number of lines in each sheet including header
S_No = 1
i = 1
While i < LastRow
CWS.Range("1:" & LineNo).Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Partition " & S_No
Sheets("Partition " & S_No).Range("A1").PasteSpecial
CWS.Range("2:" & LineNo).Delete Shift:=xlUp
LastRow = CWS.Range("A" & Rows.Count).End(xlUp).Row
S_No = S_No + 1
Wend
Macro 2: Will keep copied rows in source sheet.
Dim CWS As Worksheet
Dim LastRow As Long
Dim S_No As Long
Dim LineNo As Long
Set CWS = Sheets("Sheet2") 'Source Worksheet name
LastRow = Range("A" & Rows.Count).End(xlUp).Row
LineNo = 5 ' Number of lines in each sheet excluding header
S_No = 1
For i = 2 To LastRow
CWS.Range("1:1," & i & ":" & i + LineNo - 1).Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Partition " & S_No
Sheets("Partition " & S_No).Range("A1").PasteSpecial
i = i + Sheets("Partition " & S_No).Range("A" & Rows.Count).End(xlUp).Row - 1
S_No = S_No + 1
Next
UPDATE: I have updated the code to copy first row as a header to all sheets created. But please read the comment next to the LineNo variable.