User Profile in VBA - excel

I have some code which I want to share with other users and so when they run the workbook from their Downloads folder, I want each user to be able to run it. Basically username1 will change for each user.
I tried the examples -> HERE
Here is the code I have:
Sub CopyTime()
Dim rFind As Range
Dim rDelete As Range
Dim strSearch As String
Dim iLookAt As Long
Dim bMatchCase As Boolean
Dim WS7 As Worksheet
Const csvFile = "C:\Users\username1\Downloads\Login_Logout_Report.csv"
Dim ws As Worksheet, csv As Workbook, cCount As Long, cName As String
Dim wb1 As Excel.Workbook
Set wb1 = Workbooks.Open("C:\Users\username1\Downloads\ShiftTime.xlsm")
' Other code goes here
End Sub
Here is what I have tried
Const csvFile = "C:\Users\" & Environ("UserName") & "\Downloads\Login_Logout_Report.csv"
Dim ws As Worksheet, csv As Workbook, cCount As Long, cName As String
Dim wb1 As Excel.Workbook
Set wb1 = Workbooks.Open("C:\Users\" & Environ("UserName") & "\Downloads\ShiftTime.xlsm")

Related

Excel VB Workbook_Open() only works manually

I have a workbook that open another workbook and copies a range of data over. After that it does some data collection and sends an email. The issue I have is that the Workbook_Open() function to autorun this when the workbook is open does not work correctly. If I have the original workbook open and run it from the VB editor all works fine. If however, I let it run automatically it hangs and says the "Select method of the Worksheet class has failed"
Private Sub Workbook_Open()
Call send_Mail
End Sub
Private Sub send_Mail()
Dim Mail As CDO.Message
Set Mail = New CDO.Message
Dim strFilename As String: strFilename = "S:\Office\Requisition Logs\FY22\FY 2022.xlsx"
Dim wb2 As Workbook
Set wb2 = Workbooks.Open(Filename:=strFilename)
Dim vals As Variant
'Store the value in a variable:
vals = wb2.Sheets("FY2022 Log").Range("A2:AJ500").Value
'Close wb2:
wb2.Close savechanges:=False
Dim wb1 As Workbook
Set wb1 = ThisWorkbook
'Use the variable to assign a value to the other file/sheet:
wb1.Sheets("FY2022 Log").Range("A2:AJ500").Value = vals
wb1.Sheets("FY2022 Log").Select
Dim x As Integer
x = 1
Dim lastRow As Integer
Dim lastCol As Integer
Dim startRow As Integer
Dim numRows As Integer
startRow = 2
.............................
I do have the Workbook_Open() in the Workbook tab of the ThisWorkbook

cant seem to set workbook as a variable

I can't seem to get my two open workbooks to set as variables. I have created a sub that opens the second one, so both now should be open.
But when I step through the code I get:
"rn-time error'438' object doesn't support this property or method.
I have tried both .Select and Activate. Both come back with errors.
have included the sub that opens the second workbook, for reference.
Sub copytoMaster()
Dim wkbk As Workbook
Dim NewFile As Variant
NewFile = "C:\Users\msheppar\Desktop\new holiday project\Master Holiday Tracker.xlsm"
If NewFile <> False Then
Set wkbk = Workbooks.Open(NewFile)
End If
End Sub
Sub CopyAndPaste()
Dim wbpast As Workbook
Dim wbcop As Workbook
Dim xlastrowcopy As Long
Dim xlastrowpast As Long
Dim xlastcolumnn As Long
Dim i As Integer
Dim j As Integer
Dim r As Integer
Dim rname As String
Set wbcop = Workbooks("Holiday form (9).xlsm")
Set wbpast = Workbooks("Master Holiday Tracker.xlsm")
'For i = 1 To xlastrowcopy
wbcop = ActiveSheet
Sheets("sheet4").Select

Macro runs faster when run from developer pane, but extremley slow when called from userform

I am run the same piece of VBA code in Excel in two different ways.
1. From the developer tab, takes less than 2 sec.
2. Calling the macro from a userform, takes over a minute and freezes
Here is the code:
Sub Import()
Dim wbSource As Workbook
Dim wbDest As Workbook
Dim TargetSheet As Worksheet
Dim c As Range
Dim rng As Range
Dim i As Integer
Dim MyRange As Range
Dim SourceSheet As Worksheet
Dim source As String
Dim dest As String
Dim r As Range
Dim msg As String
source = Worksheets("Set-Up").Range("B11")
dest = Worksheets("Set-Up").Range("B8")
Set wbSource = Workbooks(source)
Set SourceSheet = wbSource.Worksheets("HFL01 Extract")
Set wbDest = Workbooks(dest)
Set TargetSheet = wbDest.Worksheets("INPUTS1")
With SourceSheet.Range("A1").CurrentRegion
For Each r In TargetSheet.Range("A1:N1") 'Range("A1:cc1")
Set c = .Rows(1).Find(r.Value, , , xlWhole, , 0)
If Not c Is Nothing Then
.Columns(c.Column).Copy
r.PasteSpecial xlPasteValues
End If
Next
Application.CutCopyMode = False
End With
Set fileDialog = Nothing
Set wbSource = Nothing
End Sub
Why does running the macro in different ways cause it to be slow? How do I make the macro run at the same speed when called from the userform?

VBA - Retrieving Data from Closed Excel Workbook

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

Acting upon a different sheet

I have a button in a sheet named "INPUT". However, the code should be acting upon data located in a different sheet named "SUMBER" when I click that button. How do I do this?
Dim osh As Worksheet
Dim iRow As Long
Dim iCol As Long
Dim iFirstRow As Long
Dim iTotalRows As Long
Dim iStartRow As Long
Dim iStopRow As Long
Dim sSectionName As String
Dim rCell As Range
Dim owb As Workbook
Dim sFilePath As String
Dim iCount As Integer
Dim Response As Integer
Dim sFName As String
iCol = Worksheets("input").Range("B4")
iRow = Worksheets("input").Range("B5")
iFirstRow = iRow
Set osh = Application.ActiveSheet
Set owb = Application.ActiveWorkbook
iTotalRows = osh.UsedRange.Rows.Count
sFilePath = Application.ActiveWorkbook.Path
I've been trying to change these lines:
Set osh = Application.ActiveSheet
Set owb = Application.ActiveWorkbook
But it has not worked.
declare and set workbook and your sheets like this:
Dim owb as Workbook
Dim oshInput, oshSumber as Worksheet
Set owb = Thisworkbook
Set oshInput = owb.Sheets("Input")
Set oshSumber = owb.Sheets("Sumber")
then add what you want to do.
For example your line:
iCol = Worksheets("input").Range("B4")
iRow = Worksheets("input").Range("B5")
once you did the above declaration and set will be:
iCol = oshInput.Range("B4")
iRow = oshInput.Range("B5")
hope this helps and get's you started a bit.

Resources