I had a working query but it was grabbing all users in AD and I am attempting to narrow this down to the last 90 days. The problem is that I no longer have any outputs even though the query does run. I know that my math is wrong and does not take off 90 days. Can anyone offer assistance with this?
Dim currentDate
currentDate = DateDiff("s", CDate("1/1/1970"), Now()) * 1000#
currentDate = currentDate - 7776000000# 'Subtracts 90 days
'Does the query
objCommand.CommandText = _
"<LDAP://" & strDN & ">;" & _
"(&(objectclass=user)(objectcategory=person)(lastLogonTimestamp<=" & currentDate & "));" & _
"adspath,distinguishedname,sAMAccountName,lastLogonTimestamp,DisplayName,WhenCreated,userAccountControl;subtree"
'Output the query info
Set objRecordSet = objCommand.Execute
rngOut.CurrentRegion.Offset(2).ClearContents
While Not objRecordSet.EOF
rngOut.value = objRecordSet.Fields("DisplayName").value
Set rngOut = rngOut.Offset(0, 1)
rngOut.value = objRecordSet.Fields("sAMAccountName").value
Set rngOut = rngOut.Offset(0, 1)
rngOut.value = objRecordSet.Fields("WhenCreated").value
Set rngOut = rngOut.Offset(0, 1)
On Error Resume Next
Set objDate = objRecordSet.Fields("lastLogonTimestamp").value
If (Err.Number <> 0) Then
On Error GoTo 0
dtmDate = ""
Else
On Error GoTo 0
lngHigh = objDate.HighPart
lngLow = objDate.LowPart
If (lngLow < 0) Then
lngHigh = lngHigh + 1
End If
If (lngHigh = 0) And (lngLow = 0) Then
dtmDate = ""
Else
dtmDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
+ lngLow) / 600000000) / 1440
End If
End If
rngOut.value = dtmDate
Set rngOut = rngOut.Offset(0, 1)
rngOut.value = objRecordSet.Fields("distinguishedName").value
Set rngOut = rngOut.Offset(0, 1)
Set Uservar = objRecordSet.Fields("userAccountControl")
If Uservar And 2 Then
rngOut.value = "Disabled"
rngOut.Font.ColorIndex = 3
Else
rngOut.value = "Enabled"
rngOut.Font.ColorIndex = 0
End If
Set rngOut = rngOut.Offset(1, -5)
objRecordSet.MoveNext
Wend
I have this working but only when I change (lastLogonTimestamp<=" & currentDate & ")); to (lastLogon<=" & currentDate & ")); and for what I am wanting this does not display the correct user base. Can anyone tell me why?
As documented in the VBScript tag wiki VBScript doesn't expand variables inside strings, so you need to change this:
objCommand.CommandText = _
"<LDAP://" & strDN & ">;" & _
"(&(objectclass=user)(objectcategory=person)(lastLogonTimestamp>=currentDate));" & _
"adspath,distinguishedname,sAMAccountName,lastLogon,DisplayName,WhenCreated,userAccountControl;subtree"
into this:
objCommand.CommandText = _
"<LDAP://" & strDN & ">;" & _
"(&(objectclass=user)(objectcategory=person)(lastLogonTimestamp>=" & currentDate & "));" & _
"adspath,distinguishedname,sAMAccountName,lastLogon,DisplayName,WhenCreated,userAccountControl;subtree"
Also, I'd recommend calculating currentDate as a normal Date value, e.g. like this:
maxAge = 30 'days
currentDate = Now - maxAge
or like this:
maxAge = 30 'days
currentDate = DateAdd("d", -maxAge, Now)
and then convert it to an integer8 value using this code from Richard L. Mueller:
Function DateToInt8(d)
biasKey = CreateObject("Wscript.Shell").RegRead("HKLM\System" & _
"\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
If (UCase(TypeName(biasKey)) = "LONG") Then
bias = biasKey
ElseIf (UCase(TypeName(biasKey)) = "VARIANT()") Then
bias = 0
For k = 0 To UBound(biasKey)
bias = bias + (biasKey(k) * 256^k)
Next
End If
DateToInt8 = CStr(DateDiff("s", #1/1/1601#, DateAdd("n", bias, d))) & "0000000"
End Function
...
objCommand.CommandText = "<LDAP://" & strDN & ">;" & _
"(&(objectclass=user)(objectcategory=person)(lastLogonTimestamp>=" & _
DateToInt8(currentDate) & "));adspath,distinguishedname,sAMAccountName," & _
"lastLogon,DisplayName,WhenCreated,userAccountControl;subtree"
Related
I am trying to write a VBA macro for dragging and dropping a VLOOKUP function so that each row can correspond to the cell being referenced within that row in the VLOOKUP function. Here is the code I wrote to try and reference the corresponding cell address:
Sub ReferToCell()
Dim WB As Workbook
Set WB = ThisWorkbook
Dim LCSheet As Worksheet
Set LCSheet = WB.Worksheets("Livemap Current")
LCROw2 = LCSheet.Cells(LCSheet.Rows.Count, 2).End(xlUp).Row 'Get new last row of Current
LCSheet.Range(LCSheet.Cells(3, 8), LCSheet.Cells(LCROw2, 8)) = "=IF(ISBLANK(F" & x & "), 99, VLOOKUP(F" & x & ",'Permit Tracker BACKUP'!D:E, 2, 0)) + VLOOKUP(A" & x & ",'Permit Tracker BACKUP'!G:H, 2, 0)" 'not sure what it's supposed to do.
LCSheet.Range(LCSheet.Cells(3, 9), LCSheet.Cells(LCROw2, 9)) = "=IF(AND(F" & x & "<>"""",OR(A" & x & "=""CORE Permits"", A" & x & "=""NED Permits"", A" & x & "=""Anchor & Groundbed Permits"", A" & x & "=""Pole Placement Permits"")), IF(COUNTIFS(E$3:E" & x & ",E" & x & ",F$3:F" & x & ",F" & x & ")>1, """", 1), """")"
LCSheet.Range(LCSheet.Cells(3, 10), LCSheet.Cells(LCROw2, 10)) = "=IFERROR(INDEX('WORK ORDER REPORT'!B:B, MATCH(B3, 'WORK ORDER REPORT'!A:A, 0), 1) & """", """")" 'not sure what it's supposed to do.
LCSheet.AutoFilter.Sort.SortFields.Clear
LCSheet.AutoFilter.Sort.SortFields.Add2 _
Key:=Range("H2:H" & LCROw2), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
With LCSheet.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
This is what I get in return:
Column H and J get an error message
Basically, I want to get the Vlookup function to drag and drop automatically without having to type in the function, lock in the ranges, autofill and re-calculating or refreshing the formula. In other words, I want to be able to automate this task on VBA, but I know I am doing something wrong on the code. Any help would be truly appreciated it!! Thank you all in Advance!!
'Declaration of workbook variables
Dim WB _
, WB2 _
As Workbook
Set WB = ThisWorkbook 'Live_map_current
'Setting the sheet variables
Dim Nada _
, CSheet _
, LCSheet _
, PSheet _
, CoreSheet _
, NedSheet _
, InSheet _
, DefSheet _
, CXSheet _
, BSheet _
, AnSheet _
, PoSheet _
As Worksheet
Set Nada = WB.Worksheets("narrow_data")
Set CoreSheet = WB.Worksheets("CORE Permits")
Set NedSheet = WB.Worksheets("NED Permits")
Set InSheet = WB.Worksheets("Inactive Permits")
Set DefSheet = WB.Worksheets("Carved Permits")
Set CXSheet = WB.Worksheets("Construction Not Needed")
Set BSheet = WB.Worksheets("Canceled Permits")
Set AnSheet = WB.Worksheets("Anchor & Groundbed Permits")
Set PoSheet = WB.Worksheets("Pole Placement Permits")
Set LCSheet = WB.Worksheets("Livemap Current")
'Define counting variables
Dim ObIDCol _
, NameCol _
, NetCol _
, DY(1 To 5) _
, PS(1 To 5) _
, JS _
As Integer
'these variables will be assigned values for populating the Livemap Current page in the latter portion of this script.
Dim DYEAColCount _
, c _
, i _
, n _
, x _
, y _
, FindCol _
, cRow _
, NRow _
, pRow _
, pCol _
, cCol _
, VRow _
, LCRow _
, LCROw2 _
, LCCol _
, TRow _
, TOldRow _
, iLastRow _
, iLastCol _
As Long
'Variables that will filter and format the data on the Livemap current sheet in narrow (long) format.
Dim PageName _
, Hub _
, StrFile _
, UserName _
, StrFileName _
, MObIDColLetter _
, ColLetter _
As String
UserName = Environ$("Username") 'Grabs username for save directory
'Grab Permits_Export file from the desktop
StrFile = Dir("C:\Users\" & UserName & "\Desktop\*Permits*")
StrFileName = "C:\Users\" & UserName & "\Desktop\" & StrFile
Set WB2 = Workbooks.Open(StrFileName) 'opens the permits files
WB2.Worksheets("CORE Permits").Columns("A:AR").Copy CoreSheet.Columns("A:AR") 'copies the layer sheets into the new livemap file
WB2.Worksheets("NED Permits").Columns("A:AR").Copy NedSheet.Columns("A:AR")
WB2.Worksheets("Inactive Permits").Columns("A:AR").Copy InSheet.Columns("A:AR")
WB2.Worksheets("Carved Permits").Columns("A:AR").Copy DefSheet.Columns("A:AR")
WB2.Worksheets("Construction Not Needed").Columns("A:AR").Copy CXSheet.Columns("A:AR")
WB2.Worksheets("Canceled Permits").Columns("A:AR").Copy BSheet.Columns("A:AR")
WB2.Worksheets("Anchor & Groundbed Permits").Columns("A:S").Copy AnSheet.Columns("A:S")
WB2.Worksheets("Pole Placement Permits").Columns("A:S").Copy PoSheet.Columns("A:S")
WB2.Close False
StrFile = Dir
'Clear Livemap Current sheet on new workbook
Application.StatusBar = "Clear Livemap Current"
LCRow = LCSheet.Cells(LCSheet.Rows.Count, 1).End(xlUp).Row
If LCRow > 2 Then
LCSheet.Range(LCSheet.Cells(3, 1), LCSheet.Cells(LCRow, 9)).ClearContents
Else: End If
'Set PageName (attribute for pulling the column variables from each layer page--list for compiling and filtering data
LCCol = LCSheet.Cells(1, LCSheet.Columns.Count).End(xlToLeft).Column 'Get count of Pages (i.e. attributes)
For c = 1 To LCCol
PageName = LCSheet.Cells(1, c) 'list of names to grab are in "Livemap Current" sheet, row 1 (hidden)
Set CSheet = WB.Worksheets(PageName) 'Permit layer data sheets
Application.StatusBar = PageName
cRow = CSheet.Cells(CSheet.Rows.Count, 1).End(xlUp).Row 'Get last row of data sheet
cCol = CSheet.Cells(1, CSheet.Columns.Count).End(xlToLeft).Column 'Get last col of data sheet
For i = 1 To cCol
'name
If CSheet.Cells(1, i) = "OBJECTID" Then
ObIDCol = i
ElseIf CSheet.Cells(1, i) = "HUB NAME" Then
NameCol = i
'Network Class
ElseIf CSheet.Cells(1, i) = "NETWORK CLASS" Then
NetCol = i
'permit_status_1
'dyea_1
ElseIf CSheet.Cells(1, i) = "DYEA 1" Then
DY(1) = i
'permit_status_1
ElseIf CSheet.Cells(1, i) = "PERMIT STATUS 1" Then
PS(1) = i
'dyea_2
ElseIf CSheet.Cells(1, i) = "DYEA 2" Then
DY(2) = i
'permit_status_2
ElseIf CSheet.Cells(1, i) = "PERMIT STATUS 2" Then
PS(2) = i
'dyea_3
ElseIf CSheet.Cells(1, i) = "DYEA 3" Then
DY(3) = i
'permit_status_3
ElseIf CSheet.Cells(1, i) = "PERMIT STATUS 3" Then
PS(3) = i
'dyea_4
ElseIf CSheet.Cells(1, i) = "DYEA 4" Then
DY(4) = i
'permit_status_4
ElseIf CSheet.Cells(1, i) = "PERMIT STATUS 4" Then
PS(4) = i
'dyea_5
ElseIf CSheet.Cells(1, i) = "DYEA 5" Then
DY(5) = i
'permit_status_5
ElseIf CSheet.Cells(1, i) = "PERMIT STATUS 5" Then
PS(5) = i
ElseIf CSheet.Cells(1, i) = "JOB STATUS" Then
JS = i
Else: End If
Next i
If DY(2) = 0 Then
DYEAColCount = 1
Else: DYEAColCount = 5
End If
For x = 1 To DYEAColCount
For y = 2 To cRow
If CSheet.Cells(y, DY(x)) <> "" Then
LCRow = LCSheet.Cells(LCSheet.Rows.Count, 1).End(xlUp).Row 'Get last row of Current
LCSheet.Cells(LCRow + 1, 1) = PageName
LCSheet.Cells(LCRow + 1, 2) = CSheet.Cells(y, ObIDCol) 'Object ID
LCSheet.Cells(LCRow + 1, 3) = CSheet.Cells(y, NameCol)
LCSheet.Cells(LCRow + 1, 4) = CSheet.Cells(y, NetCol)
LCSheet.Cells(LCRow + 1, 5) = CSheet.Cells(y, DY(x))
LCSheet.Cells(LCRow + 1, 6) = CSheet.Cells(y, PS(x))
LCSheet.Cells(LCRow + 1, 7) = CSheet.Cells(y, JS)
Else: End If
Next y
LCROw2 = LCSheet.Cells(LCSheet.Rows.Count, 2).End(xlUp).Row 'Get new last row of Current
Next x
'Clean column data for next iteration
For n = 1 To 5
DY(n) = 0
PS(n) = 0
Next n
Next c
Debug.Print ("Copy Livemap Layer data into one set of columns: " & Format((Timer - StartTime2) / 86400, "hh:mm:ss"))
StartTime3 = Timer
'Remove rows with blank DYEAs
Application.StatusBar = "Remove blanks"
'LCSheet.Columns(4).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Debug.Print ("Remove blanks: " & Format((Timer - StartTime3) / 86400, "hh:mm:ss"))
StartTime2 = Timer
x = 3
LCROw2 = LCSheet.Cells(LCSheet.Rows.Count, 2).End(xlUp).Row 'Get new last row of Current
LCSheet.Range(LCSheet.Cells(3, 8), LCSheet.Cells(LCROw2, 8)) = "=IF(ISBLANK(F" & x & "), 99, VLOOKUP(F" & x & ",'Permit Tracker BACKUP'!D:E, 2, 0)) + VLOOKUP(A" & x & ",'Permit Tracker BACKUP'!G:H, 2, 0)" 'not sure what it's supposed to do.
LCSheet.Range(LCSheet.Cells(3, 9), LCSheet.Cells(LCROw2, 9)) = "=IF(AND(F" & x & "<>"""",OR(A" & x & "=""CORE Permits"", A" & x & "=""NED Permits"", A" & x & "=""Anchor & Groundbed Permits"", A" & x & "=""Pole Placement Permits"")), IF(COUNTIFS(E$3:E" & x & ",E" & x & ",F$3:F" & x & ",F" & x & ")>1, """", 1), """")"
LCSheet.Range(LCSheet.Cells(3, 10), LCSheet.Cells(LCROw2, 10)) = "=IFERROR(INDEX('WORK ORDER REPORT'!B:B, MATCH(B3, 'WORK ORDER REPORT'!A:A, 0), 1) & """", """")"
'Sort Status reverse alphabetical
LCSheet.AutoFilter.Sort.SortFields.Clear
LCSheet.AutoFilter.Sort.SortFields.Add2 _
Key:=Range("H2:H" & LCROw2), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
With LCSheet.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
When running this code, I get a running Error number 9, and when I click debug, it points me to this line of code:
"Set CSheet = WB.Worksheets(PageName) 'Permit layer data sheets"
The code still works up until this point:
"'Remove rows with blank DYEAs
Application.StatusBar = "Remove blanks"
'LCSheet.Columns(4).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Debug.Print ("Remove blanks: " & Format((Timer - StartTime3) / 86400, "hh:mm:ss"))
StartTime2 = Timer"
meaning that the blank rows that have blank cells in the DYEA columns are removed, but then the rest of the code is NOT executed--the part that I had trouble with and that was solved by adding "X = 3" to the formulas, which is why I am running it as a separate sub routine. So the code is ultimately not executed towards the end.
I have some a macro that assigns cataogories to various rows based on keywords.
This works well however is heavy on the machine as it is using the whole column.
How can I set it so that It will just search for these words until the last entry in column A:A?
Sub Categorise()
Sheets("Data").Range("I:I") = "=IF(OR(ISNUMBER(SEARCH(""*chair fault*"",B:B)),ISNUMBER(SEARCH(""*Chair noise*"",B:B))), ""Seating"", """")"
Sheets("Data").Range("L:L") = "=IF(OR(ISNUMBER(SEARCH(""*Arm fail*"",B:B)),ISNUMBER(SEARCH(""*Arm inhibition*"",B:B)),ISNUMBER(SEARCH(""*Gap in Arm*"",B:B)),ISNUMBER(SEARCH(""*No Arms*"",B:B)),ISNUMBER(SEARCH(""*All Arms*"",B:B))), ""Angles"", """")"
Sheets("Data").Range("M:M") = "=IF(OR(ISNUMBER(SEARCH(""*Couch*"",B:B)),ISNUMBER(SEARCH(""*Heating*"",B:B))), ""Comfort"", """")"
Sheets("Data").Range("J:J") = "=IF(OR(ISNUMBER(SEARCH(""*UDCD*"",B:B)),ISNUMBER(SEARCH(""*HDD flats*"",B:B)),ISNUMBER(SEARCH(""*HDD runner*"",B:B))), ""Runners"", """")"
Sheets("Data").Range("K:K") = "=IF(ISNUMBER(SEARCH(""*Cabbies*"",B:B)),""Cabbies four"","""")"
Sheets("Data").Range("N:N") = "=IF(ISNUMBER(SEARCH(""*Braker*"",B:B)),""Elec"","""")"
Sheets("Data").Range("O:O") = "=IF(OR(ISNUMBER(SEARCH(""*Camera*"",B:B)),ISNUMBER(SEARCH(""*chough*"",B:B)),ISNUMBER(SEARCH(""*Master MCC*"",B:B)),ISNUMBER(SEARCH(""*Standards*"",B:B)),ISNUMBER(SEARCH(""*screen*"",B:B)),ISNUMBER(SEARCH(""*RTSS*"",B:B)),ISNUMBER(SEARCH(""*Heads*"",B:B)),ISNUMBER(SEARCH(""*Harps faulty*"",B:B)),ISNUMBER(SEARCH(""*TMSC*"",B:B)),ISNUMBER(SEARCH(""*Blind*"",B:B))), ""Blinders"", """")"
Sheets("Data").Range("P:P") = "=IF(OR(ISNUMBER(SEARCH(""*faulting*"",B:B)),ISNUMBER(SEARCH(""*Marker MN*"",B:B)),ISNUMBER(SEARCH(""*Elec M5*"",B:B)),ISNUMBER(SEARCH(""* Alarm*"",B:B)),ISNUMBER(SEARCH(""*Graber*"",B:B)),ISNUMBER(SEARCH(""*catcher*"",B:B)),ISNUMBER(SEARCH(""*Circuit*"",B:B)),ISNUMBER(SEARCH(""*Sal fault*"",B:B)),ISNUMBER(SEARCH(""*Panter*"",B:B)),ISNUMBER(SEARCH(""*Vigilance*"",B:B))), ""Misc"", """")"
Sheets("Data").Range("F:F") = "=I:I&J:J&K:K&L:L&M:M&N:N&O:O&P:P"
Sheets("Data").Columns("I:P").EntireColumn.Hidden = True
Sheets("Data").Range("F1").FormulaR1C1 = "System"
End Sub
You can use the power of vba to build the formulas and save you having to typing so much, Your question said column A but your code shows column B. I have used column B,
Sub Categorise()
Dim wsData As Worksheet, lastrow As Long
Set wsData = ThisWorkbook.Sheets(1)
With wsData
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
End With
Dim ar(8), isN() As String
ar(0) = Array("Seating", "chair fault", "chair noise")
ar(1) = Array("Angles", "Arm fail", "arm inhibition", "Gap in Arm", "No Arms", "All Arms")
ar(2) = Array("Comfort", "Couch", "Heating")
ar(3) = Array("Runners", "UDCD", "HDD flats", "HDD runner")
ar(4) = Array("Cabbies four", "Cabbies")
ar(5) = Array("Elec", "Braker")
ar(6) = Array("Blinders", "camera", "chough", "Master MCC", "Standards", "screen", _
"RTSS", "Heads", "Harps faulty", "TMSC", "Blind")
ar(7) = Array("Misc", "faulting", "Marker MN", "Elec M5", "Alarm", _
"Grabber", "catcher", "Circuit", "Sal fault", "panter", "Vigilance")
With wsData
.Range("I1:I" & lastrow) = Query(ar(0))
.Range("L1:L" & lastrow) = Query(ar(1))
.Range("M1:M" & lastrow) = Query(ar(2))
.Range("J1:J" & lastrow) = Query(ar(3))
.Range("K1:K" & lastrow) = Query(ar(4))
.Range("N1:N" & lastrow) = Query(ar(5))
.Range("O1:O" & lastrow) = Query(ar(6))
.Range("P1:P" & lastrow) = Query(ar(7))
.Range("F1:F" & lastrow) = "=I:I&J:J&K:K&L:L&M:M&N:N&O:O&P:P"
.Columns("I:P").EntireColumn.Hidden = True
.Range("F1").FormulaR1C1 = "System"
End With
MsgBox "Done"
End Sub
Function Query(ar) As String
Dim isN() As String, i As Integer
ReDim isN(UBound(ar) - 1)
For i = 1 To UBound(ar)
isN(i - 1) = "ISNUMBER(SEARCH(""*" & ar(i) & "*"",B:B))"
Next
If UBound(ar) > 1 Then
Query = "=IF(OR(" & Join(isN, ",") & "), """ & ar(0) & ""","""")"
Else
Query = "=IF(" & isN(0) & ", """ & ar(0) & ""","""")"
End If
Debug.Print Query
End Function
I am trying to change resource in SAP via excel macro. I need to find the row number of the focused cell and then insert a 'work shift' row.
I have already tried .CurrentCellRow, .SelectedRows & .GetRowPosition but unsuccessful.
Following is code I wrote till now,
Sub SAP_Entry_Plus(i As Variant)
Dim STime As String
Dim FTime As String
Dim CU As String
Session.findById("wnd[0]/tbar[1]/btn[26]").press
SlcDate = ThisWorkbook.Worksheets("Planned Shifts").Range("C" & i).Value
x = (Weekday(SlcDate, vbMonday) - 1)
MonDate = SlcDate - x
Session.findById("wnd[1]/usr/ctxtRC68K-DATUV_SEL").Text = MonDate
Session.findById("wnd[1]/tbar[0]/btn[0]").press
RNum2 = Session.findById("wnd[0]/usr/tblSAPLCRK0TC116").CurrentCellRow
RNum3 = Session.findById("wnd[0]/usr/tblSAPLCRK0TC116").SelectedRows
RNum4 = Session.findById("wnd[0]/usr/tblSAPLCRK0TC116").GetRowPosition
RNum5 = Session.findById("wnd[0]/usr/tblSAPLCRK0TC116").GetSelectedCellRow
RNum6 = Session.findById("wnd[0]/usr/tblSAPLCRK0TC116").GetCurrentCellRow
Session.findById("wnd[0]/usr/tblSAPLCRK0TC116").getAbsoluteRow(123).Selected = True
Session.findById("wnd[0]/usr/tblSAPLCRK0TC116/ctxtKAZA-KKOPF[2,6]").SetFocus
Session.findById("wnd[0]/tbar[1]/btn[6]").press
STime = Format(ThisWorkbook.Worksheets("Planned Shifts").Range("D" & i).Value, "hh:mm:ss")
FTime = Format(ThisWorkbook.Worksheets("Planned Shifts").Range("E" & i).Value, "hh:mm:ss")
CU = ThisWorkbook.Worksheets("Planned Shifts").Range("F" & i).Value
Session.findById("wnd[0]/usr/tblSAPLCRK0TC116/ctxtKAZA-BEGZT[8," & x + 1 & "]").Text = STime
Session.findById("wnd[0]/usr/tblSAPLCRK0TC116/ctxtKAZA-ENDZT[9," & x + 1 & "]").Text = FTime
Session.findById("wnd[0]/usr/tblSAPLCRK0TC116/txtKAZA-NGRAD[11," & x + 1 & "]").Text = CU
End Sub
You could try to solve with the following parameters:
set myTable = session.findById("wnd[0]/usr/tblSAPLCRK0TC116")
myRow = myTable.CurrentRow
myNumber_of_Rows = myTable.RowCount
myVis_Rows = myTable.VisibleRowCount
myPosition = myTable.VerticalScrollbar.Position
myAbsolute_Row = myPosition + myRow
The following link might also help a bit:
https://documentation.microfocus.com/help/index.jsp?topic=%2Fcom.borland.silktest.silk4net.doc%2Flangref%2FSAP%2FSapTableClass_ref.html
I got a macro that runs every 30 seconds using Application.Ontime. Every iteration creates a new csv file containing 8 columns and between 50 to 100 rows. The Application.Ontime normally runs from 8am to 5pm.
The problem is that sometimes during the day the macro just stop storing data in the csv files but still creates csv files. Hence, it still creates csv files but without any data in it.
EDIT:
The files that are created contain headers (StdTenorArray(0))
The variable StdXXXXXX is defined in another macro (button) and is a global variable
Here is the code:
Sub RunOnTime()
Application.CutCopyMode = False
Set ThisWkb = ThisWorkbook
dTime = Now + TimeSerial(0, 0, 30)
Application.OnTime dTime, "RunOnTime"
Call csvFileArray
Set ThisWkb = Nothing
End Sub
The above code calls this macro:
Sub csvFileArray()
Dim StdTenorArray(), ArkArr(5) As Variant
Dim FileName As String
Dim StdTenorCnt, h, j As Long
Set ThisWkb = ThisWorkbook
Application.CutCopyMode = False
ArkArr(1) = "XXXXXXctb"
ArkArr(2) = "XXXXXctb"
ArkArr(3) = "XXXXctb"
ArkArr(4) = "XXXctb"
ArkArr(5) = "XXctb"
ReDim StdTenorArray(ThisWkb.Sheets(ArkArr(1)).Range("B" & Rows.Count).End(xlUp).Row + ThisWkb.Sheets(ArkArr(2)).Range("B" & Rows.Count).End(xlUp).Row + ThisWkb.Sheets(ArkArr(3)).Range("B" & Rows.Count).End(xlUp).Row + ThisWkb.Sheets(ArkArr(4)).Range("B" & Rows.Count).End(xlUp).Row + ThisWkb.Sheets(ArkArr(5)).Range("B" & Rows.Count).End(xlUp).Row)
'Standard tenors
StdTenorCnt = 1
If StdXXXXXX = True Then
For j = 5 To 29
If UCase(ThisWkb.Sheets(ArkArr(1)).Cells(j, 4)) = True _
And WorksheetFunction.IsNumber(ThisWkb.Sheets(ArkArr(1)).Cells(j, 10)) = True _
And WorksheetFunction.IsNumber(ThisWkb.Sheets(ArkArr(1)).Cells(j, 11)) = True _
And IsDate(ThisWkb.Sheets(ArkArr(1)).Cells(j, 7)) = True _
And IsDate(ThisWkb.Sheets(ArkArr(1)).Cells(j, 8)) = True Then
StdTenorArray(StdTenorCnt) = "" & ThisWkb.Sheets(ArkArr(1)).Cells(j, 5) & ";" & Format(ThisWkb.Sheets(ArkArr(1)).Cells(j, 7), "yyyymmdd") & ";" & Format(ThisWkb.Sheets(ArkArr(1)).Cells(j, 8), "yyyymmdd") & ";" & ThisWkb.Sheets(ArkArr(1)).Cells(j, 9) & ";" & Replace(Format(ThisWkb.Sheets(ArkArr(1)).Cells(j, 10), "##0.00"), ",", ".") & ";" & Replace(Format(ThisWkb.Sheets(ArkArr(1)).Cells(j, 11), "##0.00"), ",", ".") & ";" & ThisWkb.Sheets(ArkArr(1)).Cells(j, 6) & ";JPD"
StdTenorCnt = StdTenorCnt + 1
End If
Next j
End I.
.
.
'more storing in the array like the part above
.
.
StdTenorArray(0) = "Symbol;SpotDate;ValueDate;Removed;Bid;Offer;Tenor;Channel"
Set fs = CreateObject("Scripting.FileSystemObject")
'test
app_path = ThisWkb.Path
Set a = fs.CreateTextFile("" & app_path & "\Test\" & Strings.Format(Now(), "dd.mm.yyyy") & " " & Strings.Format(Now(), "hh.mm.ss") & "." & Strings.Right(Strings.Format(Timer(), "#0.00"), 2) & ".csv", True)
For j = 0 To StdTenorCnt - 1
a.WriteLine ("" & StdTenorArray(j) & "")
Next j
a.Close
ThisWkb.Sheets("DKK").Cells(1, 27) = "" & Strings.Format(Now(), "hh:mm:ss") & ":" & Strings.Right(Strings.Format(Timer(), "#0.00"), 2) & ""
ReDim StdTenorArray(0)
Set fs = Nothing
Set a = Nothing
Set ThisWkb = Nothing
Application.CutCopyMode = False
End Sub
Hope someone have a solution to this problem or maybe can point me in the right direction.
Have a nice weekend.
\Kristian
I am new to objects and I have all of this working except the htm.getelementsbyid. When I get to the rows.length I get the error:
run-time error 91, with object variable not set
Sub get_correct_address()
Dim x As Long, y As Long
Dim htm As Object
Set htm = CreateObject("htmlFile")
row = 6
StrNumCol = 4
StrNamCol = 5
StrTypCol = 6
CityCol = 7
ZipCol = 3
StateCol = 8
With CreateObject("msxml2.xmlhttp")
.Open "GET", "http://production.shippingapis.com/ShippingAPITest.dll?API=Verify&XML= <AddressValidateRequest%20USERID=""968APPRA1046"">" & _
"<Address>" & _
"<Address1></Address1>" & _
"<Address2>" & CStr(Sheets("import").Cells(row, StrNumCol).value) + " " + Sheets("import").Cells(row, StrNamCol).value + " " + Sheets("import").Cells(row, StrTypCol).value & "</Address2>" & _
"<City>" & CStr(Sheets("import").Cells(row, CityCol).value) & "</City>" & _
"<State>" & CStr(Sheets("import").Cells(row, StateCol).value) & "</State>" & _
"<Zip5></Zip5>" & _
"<Zip4>" & CStr(Sheets("import").Cells(row, ZipCol).value) & "</Zip4>" & _
"</Address>" & _
"</AddressValidateRequest>" & _
""", False"
.send
htm.body.innerhtml = .responsetext
End With
With htm.getelementbyid("comps-results")
For x = 0 To .Rows.length - 1
For y = 0 To .Rows(x).Cells.length - 1
Sheets(1).Cells(x + 1, y + 1).value = .Rows(x).Cells(y).innertext
Next y
Next x
End With
'http://production.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<AddressValidateRequest%20USERID="xxxxxxxxxxxx">
End Sub
I cannot seem to open the object at the address in your code (it must be protected). That said, the most likely problem is that htm.getelementbyid("comps-results") is returning nothing. Verify the values and try stepping through your code with F8 and verify that .Rows.Length has a value. If it does not, the problem may lie in the element ("comps-result") you are referencing. Also verify that .Rows.Length is what you want. Perhaps you want Rows.Count?
With htm.getelementbyid("comps-results")
For x = 0 To .Rows.Count - 1
For y = 0 To .Rows(x).Cells.Count - 1
Sheets(1).Cells(x + 1, y + 1).Value = .Rows(x).Cells(y).innertext
Next y
Next x
End With