How can I take data from multiple sheets instead of just Sheet1?
Sub CheckRowsWithAutofilter()
Dim DataBlock As Range, Dest As Range
Dim LastRow As Long, LastCol As Long
Dim SheetOne As Worksheet, SheetTwo As Worksheet
'set references up-front
Set SheetOne = ThisWorkbook.Worksheets("Sheet1")
Set SheetTwo = ThisWorkbook.Worksheets("Sheet2")
Set Dest = SheetTwo.Cells(Last + 1, "A")
'enter code here
'identify the "data block" range, which is where
With SheetOne
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set DataBlock = .Range(.Cells(112, 7), .Cells(LastRow, LastCol))
End With
With DataBlock
.SpecialCells(xlCellTypeVisible).Copy Destination:=Dest
End With
End Sub
This should loop through each worksheet named in the vWSs variant array.
Sub CheckRowsWithAutofilter()
Dim lr As Long, lc As Long
Dim SheetTwo As Worksheet
Dim w As Long, vWSs As Variant
'set references up-front
vWSs = Array("Sheet1", "Sheet3", "Sheet4")
Set SheetTwo = ThisWorkbook.Worksheets("Sheet2")
'loop through the worksheets named in vWSs
For w = LBound(vWSs) To UBound(vWSs)
With Worksheets(vWSs(w))
lr = .Range("A" & .Rows.Count).End(xlUp).Row
lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
With .Range(.Cells(112, 7), .Cells(lr, lc))
.SpecialCells(xlCellTypeVisible).Copy _
Destination:=Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
End With
End With
Next w
End Sub
I cut out some of your variables as they were only used once and sometimes the code to declare and assign them was more than simply making the direct reference.
Related
I am trying to copy data from workbook1 and pasting to workbook2 as per there valves if the valve is not same as previous than create a new sheet in the workbook and start pasting valve in the new sheet and do until did not find blank row in workbook1.
Sub icopy()
Dim LastRow As Long, Limit2 As Long, c As Long, d As Long, erow As Long
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, wb As Workbook,
wb1 As Workbook
If Is_WorkBook_Open("test.xlsx") Then
Set wb = Workbooks("test.xlsx")
Else
Set wb = Workbooks.Open("D:\Data\test.xlsx")
End If
Set sh1 = wb.Sheets("Sheet1")
LastRow = sh1.Cells(Rows.Count, 1).End(xlUp).Row
'wb.Close
MsgBox LastRow
For i = 2 To LastRow
If sh1.Cells(i, 1) = sh1.Cells(i + 1, 1) Then
If (i = 2) Then
Set wb1 = Workbooks.Open("D:\Data\Data1.xlsx")
Sheets.Add(After:=Sheets(Sheets.Count)).Name = sh1.Cells(2, 1)
Set sh2 = wb1.ActiveSheet.Name
End If
sh1.Range(Cells(i, 1), Cells(i, 3)).Copy
erow = sh2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'sh2.Cells(erow, 1).Select
sh2.Cells(erow, 3).Paste
sh2.Paste
ActiveWorkbook.Save
Else
MsgBox i
Sheets.Add(After:=Sheets(Sheets.Count)).Name = sh1.Cells(i + 1, 1)
End If
Next i
'erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'ActiveSheet.Cells(erow, 1).Select
' ActiveSheet.Paste
'ActiveWorkbook.Save
'ActiveWorkbook.Close
'Application.CutCopyMode = False
End Sub
Function Is_WorkBook_Open(ByVal strWorkbookName As String) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(strWorkbookName)
If Err Then
Is_WorkBook_Open = False
Else
Is_WorkBook_Open = True
End If
End Function
since I understand your valve data are adjacent (i.e. all same valve data are within one block of adjacent rows), you could consider the following:
Option Explicit
Sub icopy()
Dim sh1 As Worksheet, sh2 As Worksheet, wb1 As Workbook
Dim iRow As Long
If Is_WorkBook_Open("test.xlsx") Then
Set sh1 = Workbooks("test.xlsx").Sheets("Sheet1")
Else
Set sh1 = Workbooks.Open("D:\Data\test.xlsx").Sheets("Sheet1")
End If
Set wb1 = Workbooks.Open("D:\Data\Data1.xlsx") ' open your target workbook
With sh1
iRow = 2
With .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
Do While iRow <= .Rows.Count
.AutoFilter field:=1, Criteria1:=.Cells(iRow, 1).Value
wb1.Sheets.Add(After:=wb1.Sheets(wb1.Sheets.Count)).name = .Cells(iRow, 1).Text
With .Resize(, 3).SpecialCells(xlCellTypeVisible)
.copy Destination:=wb1.Sheets(.Cells(iRow, 1).Text).Range("a1")
iRow = .Areas(.Areas.Count).Rows(.Areas(.Areas.Count).Rows.Count).row + 1
End With
Loop
End With
.AutoFilterMode = False
End With
End Sub
Trying to use vba to write countifs function but got an error of object doesn't support this property or method. (Run-time error 438)
Sub counters()
Dim rng, rng2, rng3, rng4 As Range
Dim lrow, lr, lr2, lr3, lr4 As Long
Dim ws, ws1 As Worksheet
Set ws = Sheets("Data")
Set ws1 = Sheets("Table E-1 Zip Codes PIF")
lr = ws.Cells(Cells.Rows.Count, "D").End(xlUp).Row
lr2 = ws.Cells(Cells.Rows.Count, "X").End(xlUp).Row
lr3 = ws.Cells(Cells.Rows.Count, "Y").End(xlUp).Row
lr4 = ws.Cells(Cells.Rows.Count, "AE").End(xlUp).Row
lrow = ws1.Cells(Cells.Rows.Count, "B").End(xlUp).Row
'zip code
Set rng = ws.Range("D2:D" & lr)
'county
Set rng2 = ws.Range("X2:X" & lr2)
'Region
Set rng3 = ws.Range("Y2:Y" & lr3)
'policy form
Set rng4 = ws.Range("AE2:AE" & lr4)
For i = 5 To lrow - 1
Worksheets("Table E-1 Zip Codes PIF").Cells(i, 4).Value = Application.WorksheetFunction.CountIfs(ws.rng, ws1.Cells(i, 2).Value, ws.rng2, ws1.Cells(i, 3).Value, ws.rng3, "NW", ws.rng4, "Basic Choice")
Next i
End Sub
Excel function works fine but need to use vba to automate the process. Tried recording the macro but it gives reference and wasn't sure how to re-write codes for ranges (ws.Range("x2:x" & lr)). All of the rng have the same amount of data (rows) so wasn't sure if I need to define it each time.
I want my result to be from D5 to N-1 in Table E-1... sheet. I used For i = 5 To lrow - 1 since there is a total in the last row.
In Table E-1 sheet, Column D have a list of zip codes and column E have a list of county that I am trying to match.
Except for how you use your CountIfs Function, since there are no screen-shots of your data, the code below will take care of your run-time errors.
Explanation for your run-time errors:
1.Wrong declaration, Dim ws, ws1 As Worksheet should be Dim ws As Worksheet, ws1 As Worksheet , otherwise ws is not defined as Worksheet. The same goes to all of your variables declarations.
2.Find last row in a column: lr = ws.Cells(Cells.Rows.Count, "D").End(xlUp).Row is wrong, it should be lr = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row.
3.Like #Scott Holtzman wrote in his comment, since you already set your rng in previous lines, (Set rng = ws.Range("D2:D" & lr)) , then it should be used with just rng and not ws.rng.
4.To improve and clean your code, you could just use With ws in the beginning of your code, and nest most of it related objects underneath.
"Clean" Code
Option Explicit
Sub counters()
Dim rng As Range, rng2 As Range, rng3 As Range, rng4 As Range
Dim lrow As Long, lr As Long, lr2 As Long, lr3 As Long, lr4 As Long
Dim ws As Worksheet, ws1 As Worksheet
Set ws = Sheets("Data")
Set ws1 = Sheets("Table E-1 Zip Codes PIF")
' last row in Column B in "Table E-1 Zip Codes PIF" sheet
lrow = ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row
With ws
lr = .Cells(.Rows.Count, "D").End(xlUp).Row
lr2 = .Cells(.Rows.Count, "X").End(xlUp).Row
lr3 = .Cells(.Rows.Count, "Y").End(xlUp).Row
lr4 = .Cells(.Rows.Count, "AE").End(xlUp).Row
'zip code
Set rng = .Range("D2:D" & lr)
'county
Set rng2 = .Range("X2:X" & lr2)
'Region
Set rng3 = .Range("Y2:Y" & lr3)
'policy form
Set rng4 = .Range("AE2:AE" & lr4)
For i = 5 To lrow - 1
ws1.Cells(i, 4).Value = Application.WorksheetFunction.CountIfs(rng, ws1.Cells(i, 2).Value, rng2, ws1.Cells(i, 3).Value, rng3, "NW", rng4, "Basic Choice")
Next i
End With
End Sub
I was writing a code to select all the data entries of a Workbook which I 'Open' in a range, but the compiler gives error at the very last line (set up the Range rng)
Dim wb As Workbook
Set wb = Workbooks.Open(Range("C2") & Range("C3"))
'here Range("C2") & Range("C3") contains the location of the file's path
Dim ws As Worksheet
Set ws = wb.ActiveSheet
Dim frow As Long
frow = ws.Range("A" & Rows.count).End(xlUp).Row
Dim rng As Range
Dim frow1 As Long
frow1 = ws.Cells(1, Columns.count).End(xlToLeft).Column
Set rng = wb.ActiveSheet.Range(Cells(1, 1), Cells(frow, frow1))
Try:
Dim frow As Long
frow = ws.Range("A" & ws.Rows.count).End(xlUp).Row
Dim rng As Range
Dim fcol As Long
fcol = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column
Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(frow, fcol))
Remember that if you are using a set worksheet u have to reference it in all range objects
I would really appreciate some help to find a correct approach to solve my issue.
I am attempting to loop through all worksheets (except for "Sheet 1" and "Output".
All the above referenced worksheets contain data from cell A2 to last column and last row. I need to copy all the looped ranges (one below the other) in cell C2 in my "Output" worksheet.
Also I have a unique number in A1 in all worksheets (except for "Sheet 1" and "Output" that needs to be copied into B2 in my "Output" worksheet. The trick is (which i am struggling with) the value in A1 needs to be copied down B2 in my "Output" worksheet by the number A2:last row in all my looped worksheets.
Below is my code thus far:
Sub EveryDayImShufflingData()
Dim ws As Worksheet
Dim PasteSheet As Worksheet
Dim Rng As Range
Dim lRow As Long
Dim lCol As Long
Dim maxRow As Integer
Dim x As String
Set PasteSheet = Worksheets("Output")
Application.ScreenUpdating = False
'Loop through worksheets except "Sheet 1" and "Output"
For Each ws In ActiveWorkbook.Worksheets
If (ws.Name <> "Sheet1") And (ws.Name <> "Output") And (ws.Visible = True) Then
'Select the Worksheet
ws.Select
'With each worksheet
With ws
'Declare variables lRow and lCol
lRow = .Cells(Rows.Count, 1).End(xlUp).Row
lCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
'Set range exc. VIN
Set Rng = .Range(.Cells(2, 1), .Cells(lRow, lCol))
'Paste the range into "Output" worksheet
Rng.Copy
PasteSheet.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
x = .Cells(1, 1).Value
For i = 1 To lRow
PasteSheet.Cells(i, 2).End(xlUp).Offset(1, 0) = x
maxRow = maxRow + 1
Next
Application.CutCopyMode = False
Application.ScreenUpdating = True
End With
End If
Next ws
End Sub
Any assistance would be kindly appreciated
Try this:
Sub EveryDayImShufflingData()
Dim ws As Worksheet, copyRng As Range, lRow As Long, lCol As Long, PasteSheet As Worksheet
Set PasteSheet = Worksheets("Output")
For Each ws In ActiveWorkbook.Worksheets
If (ws.Name <> "Sheet1") And (ws.Name <> "Output") And (ws.Visible = True) Then
lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
lCol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column
Set copyRng = ws.Range(ws.Cells(2, 1), ws.Cells(lRow, lCol))
copyTargetCell = PasteSheet.Cells(Rows.Count, 3).End(xlUp).Row + 1
copyRng.Copy Destination:=PasteSheet.Range("C" & copyTargetCell)
Worksheets("Output").Range("B" & copyTargetCell & ":B" & (copyTargetCell + copyRng.Rows.Count - 1)) = ws.Range("A1")
End If
Next ws
End Sub
Sub BSRange()
Set ws1 = ThisWorkbook.Worksheets("Balance")
Set ws2 = ThisWorkbook.Worksheets("Summary")
Set ws3 = ThisWorkbook.Worksheets("Cash")
Dim Lastcol As Long
Dim Lastrow As Long
Dim colname As String
Lastcol = ws1.Cells(1, ws1.Columns.Count).End(xlToLeft).Column
For i = 2 To Lastcol
With ws1
colname = Split(Cells(, i).Address, "$")(1)
Lastrow = .Cells(.Rows.Count, colname).End(xlUp).Row
End With
With ws3
Range(Cells(4, i), Cells(Lastrow, i)).Copy ws2.Cells(Rows.Count, "A").End(xlUp).Offset(1, 1)
End With
With ws1
Range(Cells(4, i), Cells(Lastrow, i)).Copy ws2.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
Next i
End Sub
The data does not copy and the compiler shows no error in the code. Also, when I try to get rid of With in the For loop, using SheetName in the prefix, then it gives me an error.
Try with these edits. I think you just need to be more careful about qualifying worksheets when you are working across multiple. For instance Cell() will call on the active worksheet, .Cells() will call on the workbook qualified in you With statement.
Sub BSRange()
Set ws1 = ThisWorkbook.Worksheets("Balance")
Set ws2 = ThisWorkbook.Worksheets("Summary")
Set ws3 = ThisWorkbook.Worksheets("Cash")
Dim Lastcol As Long
Dim Lastrow As Long
Dim colname As String
Lastcol = ws1.Cells(1, ws1.Columns.Count).End(xlToLeft).Column
For i = 2 To Lastcol
With ws1
colname = Split(.Cells(, i).Address, "$")(1)
Lastrow = .Cells(.Rows.Count, colname).End(xlUp).Row
End With
With ws3
.Range(.Cells(4, i), .Cells(Lastrow, i)).Copy ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Offset(1, 1)
End With
With ws1
.Range(.Cells(4, i), .Cells(Lastrow, i)).Copy ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
Next i
End Sub