Do Loop Until Black Cell - vba

I am almost there! But having trouble figuring out out to loop a portion of my code. See "loop portion" towards the bottom
Sub CopyPaste()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, lastrow As Long, x As Integer, ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = "Brand By Vendor "
Sheets("Brand By Vendor ").Range("A1") = "STORE"
Sheets("Brand By Vendor ").Range("B1") = "BRAND CODE"
Sheets("Brand By Vendor ").Range("C1") = "BRAND NAME"
Set sh3 = Sheets("Brand By Vendor ")
Set sh2 = Sheets("Sheet2")
Set sh1 = Sheets("Sheet1")
Sheets("Sheet1").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
sh3.Range("B2").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks _
:=False, Transpose:=False
sh2.Range("A2").Copy
sh3.Range("A2").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks _
:=False, Transpose:=False
sh3.Range("A2:A" & Cells(Rows.Count, 2).End(xlUp).Row).FillDown
sh2.Activate
Range("A2").Select
'loop portion
Sheets("Sheet1").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
sh3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).PasteSpecial xlPasteValues
sh2.Activate
ActiveCell.Offset(1, 0).Copy
sh3.Activate
sh3.Range("A1").End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues
lastrow = Range("B" & Rows.Count).End(xlUp).Row
ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":A" & lastrow)
End Sub
I want the loop portion to execute until the there is a blank cell in sh2 in Column A. Thank you guys for your help!

Should the post title say "Do Loop Until Blank Cell"? The description says Blank and the title Black. If Blank, set the range that you will like to loop through Sheet2.Range("A:A") for instance. Define your conditional, in your case you could use IsEMpty() and then do your stuff:
Illustrative:
Dim loopRange As Range
Dim c As Range
Set loopRange = Sheet2.Range("A:A")
For Each c In loopRange
If IsEmpty(c) = False Then
'[Your code block to do stuff within the loop goes here]
End If
Next c
Is recomendable to shorten the range not to loop through all the cells in column A.
Using your code:
Dim sh2LoopRange As Range
Dim c As Range
Dim Sh2LastRow as long
'Find the last row in sheet 2 colum A with content
sh2Lastrow = sh2.Range("A" & sh2.Rows.Count).End(xlUp).Row
'Define the range to run the loop through
Set sh2LoopRange = sh2.Range("A1:A" & sh2Lastrow)
'Iterate through the range
For Each c In sh2LoopRange
'If c is not empty it will do stuff
If IsEmpty(c) = False Then
'[Your code block to do stuff within the loop goes here]
End If
Next c

Related

Excel Macro -Cells() why fail?

I am very new to macro . Iam using this code for concatenating two column values into one column. This code today failed , for 10 first rows of the sheet , and it worked for the rest of the rows.Why happened like this , i havent changed anything at all !
Thanks.
Sub FixCrossSell()
Dim wb As Workbook
Dim lr As Long
Set wb = ThisWorkbook
wb.Worksheets("CrossSell").Activate
Cells(2, 1).Value = "=B2&E2"
lr = Cells(Rows.Count, 2).End(xlUp).Row
Range("A2").Select
Selection.Copy
Range("A3:A" & lr).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.Calculate
Range("A2:A" & lr).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Try this:
Sub FiXCrossSell()
Dim lr As Long
With Worksheets("CrossSell")
lr = .Cells(Rows.Count, 2).End(xlUp).Row
With .Range("A2:A" & lr)
.FormulaR1C1 = "=rc2&rc5"
.Value = .Value
End With
End With
End Sub
Probably just count the cells in column B, then place the Formula in Column A
Sub Button1_Click()
Dim LstRw As Long, Rng As Range, Sh As Worksheet
Set Sh = Sheets("CrossSell")
With Sh
LstRw = .Cells(.Rows.Count, "B").End(xlUp).Row
Set Rng = .Range("A2:A" & LstRw)
Rng = "=B2&E2"
End With
End Sub
Ah.. I see someone else answered this while I was thinking about it.

VBA- Copy and filter a specific columns to a sheet from a workbook

I already got how can i copy specific column from another workbook but now i also need to filter a specific column. I have tried this code but i encounter an error "Subscript out of range".
I need to filter Column C that contains "Mary" and copy its corresponding data.
This is the sample of my code, I know there is something wrong with my syntax especially in using auto filter for COLUMN C and copying different column and paste it to another workbook. Please help me to make it right. Thanks
Sub RAWtransfertoTRUST()
Dim MainWorkfile As Workbook
Dim OtherWorkfile As Workbook
Dim TrackerSht As Worksheet
Dim FilterSht As Worksheet
Dim lRow As Long, lRw As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
' set workbook object
Set MainWorkfile = ActiveWorkbook
' set the worksheet object
Set TrackerSht = MainWorkfile.Sheets("Trust Activities Raw")
With TrackerSht
lRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
Application.AskToUpdateLinks = False
' set the 2nd workbook object
Set OtherWorkfile = Workbooks.Open(Filename:=Application.GetOpenFilename)
' set the 2nd worksheet object
Set FilterSht = OtherWorkfile.Sheets("Raw Data")
With FilterSht
.AutoFilterMode = False
.Range("B2:F").AutoFilter Field:=3, Criteria1:="Mary"
lRw = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
' paste
TrackerSht.Range("B" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row
.Range("J1:J" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("G" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("N1:Q" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("H" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("T1:W" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("L" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("Y1:Z" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("P" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("AB1:AC" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("R" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
So, a few issues here.
In this code block:
With FilterSht
.AutoFilterMode = False
.Range("B2:F").AutoFilter Field:=3, Criteria1:="Mary"
lRw = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
You are missing a number in the range B2:F. If you want to filter the entire column, then both you should exclude the number "2" from B2. I assume that you were wanting to use the lRw that is actually on the next line, so this would need to go above your range line, then you would need to include that with your B2:F by adding & lRw.
That line should now look like:
.Range("B2:F" & lRw).AutoFilter Field:=2, Criteria1:="Mary"
Also, keep in mind that this is not including row 2 in your autofilter. I assume you were wanting to filter row 2, so you would need to change it to B1: if this was the case.
Next issue is your copy / paste method. You are not pasting anything, because you never copied it. In the same With block, you can add this line: .AutoFilter.Range.Copy
Here's your final result:
Sub RAWtransfertoTRUST()
Dim MainWorkfile As Workbook, OtherWorkfile As Workbook
Dim TrackerSht As Worksheet, FilterSht As Worksheet
Dim lRow As Long, lRw As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set MainWorkfile = ActiveWorkbook
Set TrackerSht = MainWorkfile.Sheets("Trust Activities Raw")
With TrackerSht
lRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
Application.AskToUpdateLinks = False
Set OtherWorkfile = Workbooks.Open(Filename:=Application.GetOpenFilename)
Set FilterSht = OtherWorkfile.Sheets("Raw Data")
With FilterSht
.AutoFilterMode = False
lRw = .Cells(.Rows.Count, "B").End(xlUp).Row
.Range("B1:F" & lRw).AutoFilter Field:=3, Criteria1:="Mary"
.AutoFilter.Range.Copy
End With
' paste
TrackerSht.Range("B" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row
.Range("J1:J" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("G" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("N1:Q" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("H" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("T1:W" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("L" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("Y1:Z" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("P" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("AB1:AC" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("R" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
Oh, and I slightly cleaned up your code formatting :D
Thanks for all your help, i already resolved my issue. I just filter all the columns then delete the columns that i don't need. This is my sample code.
Sub RAWtransfertoTRUST()
Dim MainWorkfile As Workbook, OtherWorkfile As Workbook
Dim TrackerSht As Worksheet, FilterSht As Worksheet
Dim lRow As Long, lRw As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set MainWorkfile = ActiveWorkbook
Set TrackerSht = MainWorkfile.Sheets("Trust Activities Raw")
With TrackerSht
lRow = .Cells(.Rows.Count, "C").End(xlUp).Row
End With
Application.AskToUpdateLinks = False
Set OtherWorkfile = Workbooks.Open(Filename:=Application.GetOpenFilename)
Set FilterSht = OtherWorkfile.Sheets("Raw Data")
With FilterSht
.AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row
.Range("B1:W" & lRw).AutoFilter Field:=2, Criteria1:="Mary"
.AutoFilter.Range.Copy
End With
TrackerSht.Range("B" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With TrackerSht
.Range("G:I,K:M,R:S,X:AD").DELETE Shift:=xlToLeft
.Range("E:E").Copy
.Range("G:O").PasteSpecial Paste:=xlPasteFormats
.Range("G2", "G1000").NumberFormat = "dd/mm/yyyy"
.Range("M2", "M1000").Interior.ColorIndex = 41
.Range("J2", "J1000").Interior.ColorIndex = 6
End With
End Sub

Multiple Ranges paste into another sheet in order

Please help me with the following problem:
I have 3 ranges each on a different sheet.
I have to copy every range (till its last row with data and paste values with all of them on sheet "Rezultat" (in order so they will not paste on each other)
This is my code:
Sub MultipleRangesPaste()
Dim rng1 As Range, rng2 As Range, rng3 As Range, MultipleRng As Range
With ThisWorkbook.Sheets("REZULTAT")
Set rng1 = Sheets("NEVOI PERSONALE").Range("F2:H" & Range("H" & Rows.Count).End(xlUp).Row)
Set rng2 = Sheets("RATE").Range("F2:H" & Range("H" & Rows.Count).End(xlUp).Row)
Set rng3 = Sheets("CARDURI").Range("G2:I" & Range("I" & Rows.Count).End(xlUp).Row)
Set MultipleRng = .Range(rng1 & rng2 & rng3) ' AT THIS LINE DEBUG SAID IT IS A PROBLEM
End With
MultipleRng.Copy
With ThisWorkbook.Sheets("REZULTAT").Range("A2")
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End With
End Sub
My idea is pretty much as the one of #Shai Rado, but I did not write the whole code (that pleasure was intended for the OP) and I have a function, that locates the last used row, based on a column:
Option Explicit
Sub MultipleRangesPaste()
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim MultipleRng As Range
Dim lngRowSource As Long
Dim lngRowTarget As Long
Dim lngRows As Long
With ThisWorkbook.Sheets("REZULTAT")
Set rng1 = Sheets("NEVOI PERSONALE").Range("F2:H" & Range("H" & Rows.Count).End(xlUp).Row)
Set rng2 = Sheets("RATE").Range("F2:H" & Range("H" & Rows.Count).End(xlUp).Row)
Set rng3 = Sheets("CARDURI").Range("G2:I" & Range("I" & Rows.Count).End(xlUp).Row)
End With
rng1.Copy
With ThisWorkbook.Sheets("REZULTAT").Range("A2")
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End With
rng2.Copy
'here locate the last row of column A in ThisWorkbook.Sheets("REZULTAT") and paste from there
rng3.Copy
'here locate the last row of column A in ThisWorkbook.Sheets("REZULTAT") and paste from there
End Sub
Public Function last_row(Optional str_sheet As String, Optional column_to_check As Long = 1) As Long
Dim shSheet As Worksheet
If str_sheet = vbNullString Then
Set shSheet = ThisWorkbook.ActiveSheet
Else
Set shSheet = ThisWorkbook.Worksheets(str_sheet)
End If
last_row = shSheet.Cells(shSheet.Rows.Count, column_to_check).End(xlUp).Row
End Function
I usually use application.Union, but it doesn't work on multiple ranges from different worksheets. So in this case, you have to do it manually, copy>>paste each range, into the next available row.
Sub MultipleRangesPaste()
Dim rng1 As Range, rng2 As Range, rng3 As Range, MultipleRng As Range
Dim NextRow As Long
Set rng1 = Sheets("NEVOI PERSONALE").Range("F2:H" & Sheets("NEVOI PERSONALE").Cells(Sheets("NEVOI PERSONALE").Rows.Count, "H").End(xlUp).Row)
Set rng2 = Sheets("RATE").Range("F2:H" & Sheets("RATE").Cells(Sheets("RATE").Rows.Count, "H").End(xlUp).Row)
Set rng3 = Sheets("CARDURI").Range("G2:I" & Sheets("CARDURI").Cells(Sheets("CARDURI").Rows.Count, "I").End(xlUp).Row)
With ThisWorkbook.Sheets("REZULTAT")
' find current next empty row on Column A
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
rng1.Copy
.Range("A" & NextRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' find current next empty row on Column A
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
rng2.Copy
.Range("A" & NextRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' find current next empty row on Column A
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
rng3.Copy
.Range("A" & NextRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End With
End Sub

Copy variable range of values not formulas

I'm trying to copy a variable range from one book (Book1) to the end of a variable range of the another book (book2). I'm interested only in values of the variable range in the book 1 and this is the problem. So I need to find the last row of values (not formulas). On this forum I found several options but none of them works in my case. Here is what I got (Please see the second part of the code "Copy Detail USHB"-'Select cells to copy):
''''''Copy Detail by Vendor''''''
'Last cell in column
Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long
Set WS = Worksheets("Detail by Vendor")
With WS
Set LastCell = .Cells(.Rows.Count, "B").End(xlUp)
LastCellRowNumber = LastCell.Row + 1
End With
Dim wb As Workbook, wb2 As Workbook
Dim vFile As Variant
'Set source workbook
Set wb = ActiveWorkbook
'Open the target workbook
Workbooks.Open Filename:= _
"Book2.xlsm"
'Set selectedworkbook
Set wb2 = ActiveWorkbook
'Select cells to copy
Sheets("By Vendor").Select
Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
'Go back to original workbook you want to paste into
wb.Activate
Sheets("Detail by Vendor").Select
'Paste starting at the last empty row
wb.Worksheets("Detail by Vendor").Range("B" & LastCellRowNumber).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
'''''Copy Detail USHB'''''
'Last cell in column
Set WS = Worksheets("Detail USHB")
With WS
Set LastCell = .Cells(.Rows.Count, "B").End(xlUp)
LastCellRowNumber = LastCell.Row + 1
End With
'Activate the target workbook
wb2.Activate
'Select cells to copy
Sheets("Detail USHB").Select
Dim jLastRow As Long
jLastRow = Columns("B").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range(Selection, ActiveCell.SpecialCells(xlLastRow).Select
Selection.Copy
'Go back to original workbook you want to paste into
wb.Activate
Sheets("Detail USHB").Select
'Paste starting at the last empty row
wb.Worksheets("Detail USHB").Range("B" & LastCellRowNumber).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Following your comments, I believe you are trying to do the following:
'...
'''''Copy Detail USHB'''''
Dim D As Range
Dim S As Range
With wb2.Worksheets("Detail USHB")
'Locate the last non-blank value in source range
LastRow = .Range("B:B").Find(What:="*", _
LookIn:=xlValues, _
SearchDirection:=xlPrevious).Row
'Set range
Set S = .Range("B2:B" & LastRow)
End With
With wb.Worksheets("Detail USHB")
'Find last used cell in destination range
Set D = .Range("B" & .Rows.Count).End(xlUp)
'Offset to next row, and resize appropriately
Set D = D.Offset(1, 0).Resize(LastRow - 1, 1)
End With
'Copy values
D.Value = S.Value
End Sub

Simples repeat macro in Excel

I've googled this but couldn't find a clear answer.
I have a workbook that contains lots of sheets, each sheet contains purchase order info.
I want to copy the same cell range from each sheet and compile a long list of all of those ranges.
my codes is currently;
Sub WorksheetLoop()
Sheets("5040001253").Select
Range("A4:O23").Select
Selection.Copy
Sheets("PO_Combi").Select
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
ActiveSheet.Range("A" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
My question is: is there a simple command that allows to replace the sheet named 5040001253 with that will make this macro repeat on all sheets? If not, could someone tell me what to do to make it so?
Next code copies Range("A4:O23") from each sheet (except PO_Combi) to column A of sheet PO_Combi:
Sub WorksheetLoop()
Dim sh As Worksheet
Dim shCombi As Worksheet
Dim lastrow As Long
Set shCombi = ThisWorkbook.Worksheets("PO_Combi")
For Each sh In ThisWorkbook.Worksheets
With shCombi
If sh.Name <> .Name Then
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
sh.Range("A4:O23").Copy
.Range("A" & lastrow + 1).PasteSpecial xlPasteValues
End If
End With
Next
Application.CutCopyMode = False
End Sub