errors pulling key information from multiple excel workbooks - vba

I am attempting to write a macro in a workbook whose purpose is to display the key information from each of a set of excel files. the first column contains the name of the file which will be used in the code.
the code I have written so far should loop through the list of 11 file names in the summary sheet and pull the info called from cell E21 in each of those files and place it in cell Hx in the summary sheet.
I have had no luck getting it to work so far, my first error im getting is "invalid Qualifier" on the line that says "MySheet". I know that there are alot of other mistakes here as I have never attempted to write a sub that pulls from other closed workbooks.
My code is as follows:
Option Explicit
Sub OEEsummmary()
Dim Gcell As Range
Dim Txt$, MyPath$, MyWB$, MySheet$
Dim myValue As Integer
Dim x As Long
Dim v As Variant, r As Range, rWhere As Range
MyPath = "L:\Manufacturing Engineering\Samuel Hatcher\"
x = 2
MySheet = ActiveSheet.Name
Application.ScreenUpdating = False
Do While MySheet.Range("A" & x).Value <> ""
MyWB = MySheet.Range("A" & x).Txt
Workbooks.Open Filename:=MyPath & MyWB
Set Gcell = ActiveSheet.Range("E21")
With ThisWorkbook.ActiveSheet.Range("A" & x)
.Value = "Item"
.Offset(7, 0).Value = Gcell.Value
End With
ActiveWorkbook.Close savechanges:=False
x = x + 1
Loop
End Sub
Ive looked at what an invalid qualifier error is and i dont understand what i have wrong with that part of my code. Any help with this and any other blinding errors would be greatly appreciated!

The issue I see that's causing the Invalid Qualifier error is that you are declaring MySheet as a string, but trying to use it as a Worksheet object. Below I've declared it as a worksheet and set it to the Activesheet. I also changed the ThisWorkbook.ActiveSheet reference to MySheet, which I think is what you want. Also changed Txt to Text:
Sub OEEsummmary()
Dim Gcell As Range
Dim MySheet As Worksheet
Dim Txt$, MyPath$, MyWB$
Dim myValue As Integer
Dim x As Long
Dim v As Variant, r As Range, rWhere As Range
MyPath = "L:\Manufacturing Engineering\Samuel Hatcher\"
x = 2
Set MySheet = ActiveSheet
Application.ScreenUpdating = False
Do While MySheet.Range("A" & x).Value <> ""
MyWB = MySheet.Range("A" & x).Text
Workbooks.Open Filename:=MyPath & MyWB
Set Gcell = ActiveSheet.Range("E21")
With MySheet.Range("A" & x)
.Value = "Item"
.Offset(7, 0).Value = Gcell.Value
End With
ActiveWorkbook.Close savechanges:=False
x = x + 1
Loop
End Sub

Related

Iterate through Data Validation List

i created a button which is this macro on it:
Sub loopthroughvalidationlist()
Dim inputRange As Range
Dim c As Range
Set inputRange = Evaluate(Range("A9").Validation.Formula1)
path = "C:\test\"
For Each c In inputRange
filename1 = Range("B18").Text
ActiveWorkbook.SaveCopyAs filename:=path & filename1 & ".xlsm"
Next c
End Sub
the list of the validation contains about 5 names. but for some reason it only saves 1 file.
As far as you are not having problems with accessing the data validation list, I have hardcoded it in Range("A1:A5").
I have changed ActiveWorkbook with ThisWorkbook to see how it works as well.
-ThisWorkbook.Save is needed to produce valid .FullName and valid .Path
The tricky part is to save the oldPathFull and the oldPath of the excel file and to use ThisWorkbook.SaveAs oldPathFull on every loop.
Application.DisplayAlerts = False disables the Excel prompts. You can put it outside the loop, if you feel like it.
Sub LoopThroughValidationList()
Dim inputRange As Range
Dim c As Range
Dim oldPathFull As String
Dim oldPath As String
Set inputRange = Range("A1:A5")
ThisWorkbook.Save
oldPathFull = ThisWorkbook.FullName
oldPath = ThisWorkbook.Path & "\"
For Each c In inputRange
Application.DisplayAlerts = False
ThisWorkbook.SaveCopyAs Filename:=oldPath & c & ".xlsb"
ThisWorkbook.SaveAs oldPathFull
Application.DisplayAlerts = True
Next c
End Sub
Once you are able to get the code running with predefined values, this is a [MCVE] example how to get the data validation values of a cell B1:
Public Sub TestMe()
Dim myCell As Range
Dim valRules As Range
Set valRules = Evaluate(Range("A1").Validation.Formula1)
For Each myCell In valRules
Range("A1") = myCell
Debug.Print Range("A1")
Next myCell
End Sub

Run-Time Error '6' overflow (copy range from closed workbook)

I have the below code placed on my active workbook which copy's data from a closed workbook. It works fine and very fast but i can only copy up to 8 columns if I select more than that I get a
run-time error 6 - Overflow
Code:
Sub Get_Data()
Dim RngToCopy As Range
Dim wkbk As Workbook
Dim DestCell As Range
Dim myFileNames As Variant
Dim iCtr As Long
Dim testStr As String
Set DestCell = ThisWorkbook.Worksheets(1).Range("a1")
myFileNames = Array("C:\my documents\excel\book1.xlsm") ' i could add more workbooks to copy from and append on current worksheet
For iCtr = LBound(myFileNames) To UBound(myFileNames)
testStr = ""
On Error Resume Next
testStr = Dir(myFileNames(iCtr))
On Error GoTo 0
If testStr = "" Then
MsgBox myFileNames(iCtr) & " doesn't exist!"
Else
Set wkbk = Workbooks.Open(Filename:=myFileNames(iCtr))
With wkbk.Worksheets(1)
Set RngToCopy = .Range("a2:r2", _
.Cells(.Rows.Count, "A").End(xlUp))
End With
DestCell.Resize(RngToCopy.Rows.Count, _
RngToCopy.Columns.Count).Value _
= RngToCopy.Value
Set DestCell = DestCell.Offset(RngToCopy.Rows.Count, 0)
wkbk.Close savechanges:=False
End If
Next iCtr
End Sub
when I debug the line that it goes to is: where am I going wrong :(
DestCell.Resize(RngToCopy.Rows.Count, _
RngToCopy.Columns.Count).Value _
= RngToCopy.Value
the source workbook is a big file with a lot of data, I tried most of the suggestions when I researched on the error but no luck.
if it helps workbook contains 18 columns and 300k+ rows
Try a different approach, use Copy >> PasteSpecial (values only) :
' 1st: set copy range
With wkbk.Worksheets(1)
Set RngToCopy = .Range("A2:R2", .Cells(.Rows.Count, "A").End(xlUp))
End With
' 2nd: set destination range start position
Set DestCell = DestCell.Offset(RngToCopy.Rows.Count, 0)
' 3rd: use copy>>paste special (values only)
RngToCopy.Copy
DestCell.PasteSpecial xlPasteValues

Splitting an identified range of spreadsheets to a new workbook with a new name

I've been trying to come up with a way to split a workbook into separate workbooks based on identified worksheets in the workbook.
For example:
Say I had a worksheet for every letter in the alphabet.
I would want to split worksheets A through C into a new workbook named "A through C."
D through I will go into a new workbook named "D through I."
etc...
My idea would be to first insert a worksheet that in column A names the new workbook it will become and Columns b through as many columns as there are will the names of the worksheets to be copied into the new workbook.
Does anyone have an idea of how to make a macro for this? I've tried myself but have been unsuccessful.
Thank you!
I found this Macro out there. Does anyone think it can be modified to work?
Sub Test()
Dim Sh As Worksheet
Dim Rng As Range
Dim c As Range
Dim List As New Collection
Dim Item As Variant
Dim WB As Workbook
Application.ScreenUpdating = False
Set Sh = Worksheets("Sheet1")
Set Rng = Sh.Range("A2:A" & Sh.Range("A65536").End(xlUp).Row)
On Error Resume Next
For Each c In Rng
List.Add c.Value, CStr(c.Value)
Next c
On Error GoTo 0
Set Rng = Sh.Range("A1:H" & Sh.Range("A65536").End(xlUp).Row)
For Each Item In List
Set WB = Workbooks.Add
Rng.AutoFilter Field:=1, Criteria1:=Item
Rng.SpecialCells(xlCellTypeVisible).Copy WB.Worksheets(1).Range("A1")
Rng.AutoFilter
With WB
.SaveAs ThisWorkbook.Path & "\" & Item & ".xls"
.Close
End With
Next Item
Sh.Activate
Application.ScreenUpdating = True
End Sub
The following code assumes you have your control sheet (named "Split Parameters") in the workbook containing the macro, and it is set out with the desired filenames in column A, and the sheets that you wish to copy into that file (from the ActiveWorkbook, which might, or might not, be the one containing the macro) listed in columns B, C, etc. Row 1 is assumed to be headings, and is therefore ignored.
Sub SplitBook()
Dim lastRow As Long
Dim LastColumn As Long
Dim srcWB As Workbook
Dim newWB As Workbook
Dim i As Long
Dim c As Long
Dim XPath As String
Dim newName As String
Dim sheetName As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set srcWB = ActiveWorkbook
XPath = srcWB.Path
With ThisWorkbook.Worksheets("Split Parameters")
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
'Take the first worksheet and create a new workbook
sheetName = .Cells(i, "B").Value
srcWB.Sheets(sheetName).Copy
Set newWB = ActiveWorkbook
'Now process all the other sheets that need to go into this workbook
LastColumn = .Cells(i, .Columns.Count).End(xlToLeft).Column
For c = 3 To LastColumn
sheetName = .Cells(i, c).Value
srcWB.Sheets(sheetname).Copy After:=newWB.Sheets(newWb.Sheets.Count)
Next
'Save the new workbook
newName = .Cells(i, "A").Value
newWB.SaveAs Filename:=xPath & "\" & newName & ".xls", FileFormat:=xlExcel8
newWB.Close False
Next
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

using match funtion for 2 different workbooks

i have been assigned to use the .match function in vba, to compare 2 different columns in 2 different workbooks.
here is my code so far.. how do i use the match function to my goal ?
Sub Ob_match()
Dim swb As Workbook, dwb As Workbook
Dim sws As Worksheet, dws As Worksheet
Dim oCell As Range, oMatch As Range
Set swb = ActiveWorkbook
Set sws = swb.Sheets("Item")
Set dwb = Workbooks.Open(swb.Path & "\EPC_EndItem.xlsm", ReadOnly:=True)
Set dws = dwb.Sheets("Data")
If Not oMatch Is Nothing Then
oCell.Offset(0, 1) = "Y"
Else
oCell.Offset(0, 1) = ""
End If
Next oCell
MsgBox "Processing completed"
End Sub
To run this code you should be on your your first workbook and second work-book should be open in background, I find this as an easier method than to call workbook using it's address, you may change that if you like
Sub vl()
Dim lastrow As Long
Sheets("Items").Select
lastrow = Range("B" & Rows.Count).End(xlUp).Row
Range("C2:C" & lastrow).Formula = "=IF(VLOOKUP(RC2,[Book2]Data!C4,1,FALSE), ""OK"","""")"
End Sub
Here I assumed that Name of your second book is Book2.
Change it to whatever it is in the code.
Hope this helps :)

Macro Compilation error

I have a excel sheet "b.xls" in which column A and column B have content like this:
Column A Column B
C1 F1
C2 F2
C3 Z3
I want to copy content at location C1 , C2 , and C3 of sheet "Sheet1" of "b.xls" workbook to location F1 , F2 , Z3 of sheet "Sheet1" of workbook "a.xlsm"
I have wriiten following macro. On runing type mismatch error is displayed at point srcAddress in line of GetData.
Please help
Requirement is copy data without opening b.xls.
Sub Update_Data()
Dim rngA As Range
Dim rngB As Range
Dim srcAddress As Range
Dim destAddress As Range
Dim r As Long 'row iterator
Dim MyPath As String
MyPath = ActiveWorkbook.Path
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
Set rngA = Range("A2", Range("A2").End(xlDown))
Set rngB = rngA.Offset(0, 1)
For r = 1 To rngA.Rows.Count
Set srcAddress = Range(rngA(r).Value)
Set destAddress = Workbooks("a.xlsm").Sheets("Test_data").Range(rngB(r).Value)
GetData MyPath & "b.xls", "Sheet1", srcAddress, destAddress, True, True
'destAddress.Value = srcAddress.Value
Next
End Sub
I just wrote the following code - but then read that b.xls should not be opened, so it might not be what you're after. However, if you're not allowed to open b.xls, how can you access the mapping in b.xls?
anyway, here's the code, maybe you can use parts of it. It prevents screen updating, so the user will not see that another file is opened:
Sub UpdateData()
Dim rngSource As Range
Dim wbTarget As Workbook
Dim wsTarget As Worksheet
Application.ScreenUpdating = False 'This will prevent the workbook to be displayed during execution
On Error Resume Next
Set wbTarget = Workbooks("a.xls")
If Err.Number Then
Err.Clear
Set wbTarget = Workbooks.Open(YourPath & "a.xls")
End If
Set wsTarget = wbTarget.Worksheets("Sheet1")
With Worksheets("Sheet1")
For Each rngSource In .Range("A1").Resize(.Range("A" & .Rows.Count).End(xlUp).Row)
wsTarget.Range(rngSource.Offset(, 1).Value) = .Range(rngSource.Value).Value
Next
End With
wbTarget.Save
wbTarget.Close
Application.ScreenUpdating = True
End Sub