i have a code that loops through a range of cells and for each name in that range it will add a new sheet and name the new sheet after whatever is in the cell. the line of code "ws.Name = Employee_name" is giving me a 1004 runtime error "Application-Defined or Object-Defined error" it is only giving this error the second time through the look it is able to rename the sheet for the first loop. any ideas on how to prevent this?
Option Explicit
Sub read_WFH_dockets()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim WB As Workbook
Dim ws As Worksheet
Dim Employee_name As String
Set WB = ActiveWorkbook
Dim Rng_Employees As Range
Dim EmployeeFAB
Dim numrows As Long
Dim Fab As String
numrows = Range("A100000").End(xlUp).Row
Set Rng_Employees = Range("B1:B" & numrows)
For Each EmployeeFAB In Rng_Employees.Cells
Employee_name = Range("A" & EmployeeFAB.Row)
Fab = EmployeeFAB.Value
Set ws = WB.Sheets.Add
ws.Name = Employee_name
Set ws = Nothing
Employee_name = ""
Fab = ""
Next
End Sub
I'm facing a run-time error '424' on the 'For Each Cell In rg' line. Could anyone help me with this please?
Sub extract()
Dim i As Integer
Dim OpenBook As Workbook
Dim FileLocation As String
Dim rg, Cell As Range
FileLocation = "C:\\xxxxx"
Set OpenBook = Application.Workbooks.Open(FileLocation)
Set rg = OpenBook.Sheet2.Range("B1", Sheet2.Range("B1").End(xlDown)).Cell
i = 0
For Each Cell In rg
If Sheet1.Range("G2").Value = Cell Then
i = i + 1
Sheet1.Range("G7").Offset(i, 0).Value = Cell.Offset(0, 1).Value
End If
Next Cell
End Sub
I'm trying to write a vba code that gets from an excel range the names of my chart and serie that are located on my powerpoint slide to give me the position of the corresponding datalabel. For some reason I have a Type mismatch error. I think it occurs when I pass my string variables as arguments of the 'item' method, because when I replace them by their actual string value the code works fine.
Sub datalabelposition()
Dim pp As Object
Dim sl As Object
Dim chtName As Variant
Dim serName As Variant
Dim rnge As Range
Set pp = CreateObject(class:="PowerPoint.Application")
Set sl = pp.ActivePresentation.Slides(1)
Set rnge = Worksheets("Macro").Range("J10:P29")
chtName = rnge(1, 1).Value
chtName = CStr(chtName)
serName = rnge(1, 2).Value
serName = CStr(serName)
Debug.Print chtName
Debug.Print serName
Debug.Print sl.Shapes.Item(chtName).Chart.SeriesCollection.Item(serName).Points(1).DataLabel.top
End Sub
Thanks,
I am trying to pull data from a worksheet in another workbook and it isn't working properly. I'm not getting an error in the code but it is not pulling the data from the worksheet I want but rather whatever worksheet is open when the workbook opens. I read somewhere that there is no need to activate the worksheet so I am not sure what is wrong with the following code:
Dim prfile1 As String
Dim prfile2 As String
Dim filepath As String
Dim checktotal As Integer
Dim checkrng As Range
Dim emunber As String
prfile1 = Worksheets("setup").Range("B10").Value
prfile2 = Worksheets("setup").Range("B7").Value
filepath = Worksheets("setup").Range("e10").Value
emunber = Worksheets("ReprintOld").Range("V3").Value
Workbooks.Open filepath & prfile2
Windows(prfile2).Activate
Sheets(emunber).Activate
checktotal = Workbooks(prfile2).Worksheets(emunber).Range("AE1")
With Workbooks(prfile2).Worksheets(emunber)
Set checkrng = Range(Range("U5"), Range("U" & 4 + checktotal).End(xlDown))
End With
Windows(prfile1).Activate
MsgBox emunber
MsgBox checktotal
MsgBox checkrng.Address
I am trying to create a VBA script that will gather data from four different Workbooks. For now, I am just testing the code with one Workbook, but I am receiving an error when I try to acquire the data. While I would like to retrieve the data from the four Workbooks without opening them, I will need to open them in order to find the last row of data. Here is my current code:
Public Sub GetData()
Application.ScreenUpdating = False
Dim LastRow As Integer
Dim WB As Workbook
Dim xlsPath As String
Dim xlsFilename As String
Dim SheetName As String
xlsPath = "C:\Users\a27qewt\My Documents\Document Retention\FI_DocumentRetention.xlsm"
Set WB = Workbooks.Open(xlsPath)
'Workbooks("FI_DocumentRetention.xlsm").Sheets("S&S Document Locations").Unprotect
LastRow = Workbooks("FI_DocumentRetention.xlsm").Sheets("S&S Document Locations").Cells(Rows.Count, "A").End(xlUp).Row
Workbooks("SS_Index.xlsm").Sheets("Document Index").Range(Cells(2, 1), Cells(LastRow, 5)).Value = _
Workbooks("FI_DocumentRetention.xlsm").Sheets("S&S Document Locations").Range(Cells(2, 1), Cells(LastRow, 5)).Value
WB.Close False
End Sub
I am receiving a 1004 application/object defined error in the Workbooks("FI_DocumentRetention.xlsm").Sheets("S&S Document Locations").Range... line. Any suggestions why?
You already solved your problem, but here's how I'd approach it
Public Sub GetData()
Dim LastRow As Long '<< not Integer
Dim WB As Workbook
Dim xlsPath As String
Dim xlsFilename As String
Dim SheetName As String
Dim shtSrc As Worksheet, shtDest As Worksheet, rngSrc As Range
Application.ScreenUpdating = False
xlsPath = "C:\Users\a27qewt\My Documents\Document Retention\FI_DocumentRetention.xlsm"
Set WB = Workbooks.Open(xlsPath)
Set shtSrc = WB.Sheets("S&S Document Locations")
Set shtDest = Workbooks("SS_Index.xlsm").Sheets("Document Index")
LastRow = shtSrc.Cells(shtSrc.Rows.Count, "A").End(xlUp).Row
Set rngSrc = shtSrc.Range(shtSrc.Range("A2"), _
shtSrc.Cells(LastRow, 5))
shtDest.Range("A2").Resize(rngSrc.Rows.Count, _
rngSrc.Columns.Count).Value = rngSrc.Value
WB.Close False
End Sub