I have several hundred Hex numbers (32 char long) that were pulled from a sql db. I have them stored in an excel table and need to convert them to GUID with dashes. I have found an online converter, but it only does one at a time and this would be very time consuming (http://www.windowstricks.in/online-windows-guid-converter). Is there a way, either in Excel with VBA or Formulas or in SQL to convert these? It is not as simple as just adding the dashes into the correct places. I've tried that and it is not what I need to have happen. An example of the Hex and the converted dash separated GUID:
Hex
6F414B9DFB178945A3641E40BC2A4AAB
C58C415E215CEC4D9B5100532573D3FA
2B0BBF00A1403E41A333C805961CEA9F
GUID converted from the Hex above
48a6c53b-941c-46e2-9964-680754f71666
ea0ba3f4-4905-4d9c-9d83-76c57bdb060a
18cea3f7-e1d1-4609-a4bc-9bf6fec6a2d4
Any help you can give would be very appreciated.
Thanks
This function converts an hexadecimal String to a formatted GUID string:
Public Function ConvHexToGuid(hexa As String) As String
Dim guid As String * 36
Mid$(guid, 1) = Mid$(hexa, 7, 2)
Mid$(guid, 3) = Mid$(hexa, 5, 2)
Mid$(guid, 5) = Mid$(hexa, 3, 2)
Mid$(guid, 7) = Mid$(hexa, 1, 2)
Mid$(guid, 9) = "-"
Mid$(guid, 10) = Mid$(hexa, 11, 2)
Mid$(guid, 12) = Mid$(hexa, 9, 2)
Mid$(guid, 14) = "-"
Mid$(guid, 15) = Mid$(hexa, 15, 2)
Mid$(guid, 17) = Mid$(hexa, 13, 2)
Mid$(guid, 19) = "-"
Mid$(guid, 20) = Mid$(hexa, 17, 4)
Mid$(guid, 24) = "-"
Mid$(guid, 25) = Mid$(hexa, 21, 16)
ConvHexToGuid = guid
End Function
The GUID to HEX is transposed as follows:
0x00112233445566778899AABBCCDDEEFF
{33221100-5544-7766-8899-AABBCCDDEEFF}
I have found an answer. Thanks to #florent. I'm not sure why the GUID's came out differently and still worked the first time I was running this, but I have a solution. I did this using VBA code:
Sub CreateGUID()
Dim count, GUIDConverted As String
count = 2
Do While Range("F" & count).Value <> ""
Range("F" & count).Select
GUIDConverted = Range("F" & count).Value
GUIDConverted = ConvertHexToGUID(GUIDConverted)
Range("H" & count).Value = GUIDConverted
count = count + 1
Loop
End Sub
Public Function ConvertHexToGUID(hexa As String) As String
Dim guid As String * 36
Mid$(guid, 1) = Mid$(hexa, 7, 2)
Mid$(guid, 3) = Mid$(hexa, 5, 2)
Mid$(guid, 5) = Mid$(hexa, 3, 2)
Mid$(guid, 7) = Mid$(hexa, 1, 2)
Mid$(guid, 9) = "-"
Mid$(guid, 10) = Mid$(hexa, 11, 2)
Mid$(guid, 12) = Mid$(hexa, 9, 2)
Mid$(guid, 14) = "-"
Mid$(guid, 15) = Mid$(hexa, 15, 2)
Mid$(guid, 17) = Mid$(hexa, 13, 2)
Mid$(guid, 19) = "-"
Mid$(guid, 20) = Mid$(hexa, 17, 4)
Mid$(guid, 24) = "-"
Mid$(guid, 25) = Mid$(hexa, 21, 16)
ConvertHexToGUID = guid
End Function
This worked for all of the Hex numbers that I had.
Thanks to all those who took time to make a response they did all help as I was working on this.
Related
there I have an excel VBA code that retrieves its records from an external file by month and set it according to the column heading.
However, i have an error in of application-defined or object-defined error in of the line .Range("A6").Resize(n, 23) = b
does anyone know why
code:
Sub zz()
Dim arr, c, b(), n&
Application.ScreenUpdating = False
Worksheets("Sheet2").Range("A6").AutoFilter
Workbooks.Open "C:\Users\sophia.tan\Desktop\MasterPlanData.xlsx", 0, 1
arr = Sheets("Excel").UsedRange
ActiveWorkbook.Close 0
c = Array(0, 2, 13, 14, 7, 8, 11, 1, 9, 10, 16, 17, 20, 22, 15, 30, 27, 28, 29, 3, 4, 30)
d = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23)
ReDim b(1 To UBound(arr), 1 To 23)
For i = 2 To UBound(arr)
If arr(i, 12) >= DateSerial(Year:=2017, Month:=11, Day:=1) And arr(i, 12) <= DateSerial(Year:=2017, Month:=11, Day:=31) Then
n = n + 1
For j = 1 To UBound(c)
b(n, d(j)) = arr(i, c(j))
Next
End If
Next
With Worksheets("Sheet2")
.Range("A6:T" & Rows.Count).CurrentRegion.AutoFilter field:=1, Criteria1:="<>OFM"
.Range("A6:T" & Rows.Count).CurrentRegion.SpecialCells(xlCellTypeVisible).AutoFilter field:=13, Criteria1:="<>Collar & Cuff"
.Range("A6:T" & Rows.Count).CurrentRegion.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Range("A6").Resize(n, 23) = b
.Range("A6").CurrentRegion.Sort key1:=Range("G6"), order1:=xlAscending, Header:=xlYes
.Range("A6").Select
End With
Application.ScreenUpdating = 1
End Sub
Your determination on n is subjective to the If statement. However, any unfilled values in the 'rows' of b will be vbnullstrings and will produce truly blank cells.
.Range("A6").Resize(ubound(b, 1), ubound(b, 2)) = b
Alternately,
For i = 2 To UBound(arr)
If arr(i, 12) >= DateSerial(Year:=2017, Month:=11, Day:=1) And arr(i, 12) <= DateSerial(Year:=2017, Month:=11, Day:=31) Then
n = n + 1
For j = 1 To UBound(c)
b(n, d(j)) = arr(i, c(j))
Next
End If
Next
b = application.transpose(b)
redim preserve b(lbound(b, 1) to ubound(b, 1), lbound(b, 2) to n)
b = application.transpose(b)
.Range("A6").Resize(n, 23) = b
You can only adjust the last rank of an array with ReDim when using the preserve parameter.
Try
.Range("A6").Resize(n, 23).Value = b
I have this code that works but it takes a lot of time I´m sure there is a way to optimize it I did some research but i couldn't find how. My files are really big (100mb+) so anything that makes this code faster its necessary.
lastrowLaneTemplate = Sheets("LaneTemplate").Range("A65536").End(xlUp).Row
lastrowCarrier = Sheets("Routed").Range("B65536").End(xlUp).Row
lastrowCarrierd = Sheets("Routed").Range("B65536").End(xlUp).Row
j = 2
For i = 10 To lastrowLanetemplate
For z = 2 To lastrowCarrier
If Sheetlanetemplate.Cells(i, 4).Value <> "" Then
If Sheetlanetemplate.Cells(i, 4) = sheetCarrier.Cells(z, 1) And _
sheetCarrier.Cells(z, 3) = "1" Then
sheetcarrierd.Cells(j, 1) = sheetCarrier.Cells(z, 1)
sheetcarrierd.Cells(j, 2) = sheetCarrier.Cells(z, 2)
sheetcarrierd.Cells(j, 3) = sheetCarrier.Cells(z, 3)
sheetcarrierd.Cells(j, 4) = sheetCarrier.Cells(z, 4)
sheetcarrierd.Cells(j, 5) = sheetCarrier.Cells(z, 5)
sheetcarrierd.Cells(j, 6) = sheetCarrier.Cells(z, 6)
sheetcarrierd.Cells(j, 7) = sheetCarrier.Cells(z, 7)
sheetcarrierd.Cells(j, 8) = sheetCarrier.Cells(z, 8)
sheetcarrierd.Cells(j, 9) = sheetCarrier.Cells(z, 9)
sheetcarrierd.Cells(j, 10) = sheetCarrier.Cells(z, 10)
sheetcarrierd.Cells(j, 11) = sheetCarrier.Cells(z, 11)
j = j + 1
End if
Next z
Next y
As you can see that works but it takes some time and I have 10 times that.So if there is a way to do this without IF it would be perfect
My quick idea is to change 11 lines inside if...end if statement into one line:
sheetcarrierd.Range(sheetcarrierd.Cells(j, 1), sheetcarrierd.Cells(j, 11)).Value = _
sheetCarrier.Range(sheetCarrier.Cells(Z, 1), sheetCarrier.Cells(Z, 11)).Value
but I'm not sure if it would improve performance significantly.
Are you already using these?
At start:
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
At the end:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
I have several hundred cells. I want to find the latest in the grouping. For instance i have the following data:
233400-003-02
233400-002-03
233400-002-02
233400-002-01
233400-001-04
233400-001-03
233400-001-02
233400-001-01
The last number defines the revision. I want to keep only the greatest number or the latest revision. so far I have
For j = 9 To i Step 1
Dim Idstring As String
If Len(Cells(j, 1)) = 13 Then
Idstring = Left(Cells(j, 1), 10)
Cells(j, 5) = Idstring
ElseIf Len(Cells(j, 1)) = 16 Then
Idstring = Left(Cells(j, 1), 10)
Cells(j, 5) = Idstring
ElseIf Len(Cells(j, 1)) = 17 Then
Idstring = Left(Cells(j, 1), 14)
Cells(j, 5) = Idstring
ElseIf Len(Cells(j, 1)) = 20 Then
Idstring = Left(Cells(j, 1), 14)
Cells(j, 5) = Idstring
End If
If Cells(j, 5) = Cells(j - 1, 5) Then
If Len(Cells(j, 1)) = 16 Then
Cells(j, 5).EntireRow.Delete
ElseIf Len(Cells(j, 1)) = 20 Then
Cells(j, 5).EntireRow.Delete
ElseIf Right(Cells(j, 1), 1) < Right(Cells(j + 1, 1), 1) Then
Cells(j, 5).EntireRow.Delete
ElseIf Right(Cells(j, 1), 1) > Right(Cells(j + 1, 1), 1) Then
Cells(j + 1, 5).EntireRow.Delete
j = j + 1
End If
End If
Next j
What am I doing wrong? Thank you for your help.
I think your comparing to Cells(j-1) before you fill Cells(j-1). But if I'm wrong about that, you need to loop backward through the range when you delete rows or Excel loses track of where you are.
Public Sub DeleteAllButLatest()
Dim i As Long
For i = 9 To 3 Step -1
If Base(Cells(i, 1).Value) = Base(Cells(i - 1, 1).Value) Then
Cells(i, 1).EntireRow.Delete
End If
Next i
End Sub
Public Function Base(ByVal sCode As String) As String
Select Case Len(sCode)
Case 13, 17
Base = Left(sCode, Len(sCode) - 3)
Case 16, 20
Base = Left(sCode, Len(sCode) - 6)
End Select
End Function
Based on your sample data in A2:A9. Only need to go to Row 3 because Row 2 will have to be good so no need to check it. I made a function to return the "base" of each number so you can compare the base of the current cell to the cell above it. If they're the same, delete. If not, assume it's the latest.
Been following a question on this site to try and change the label backcolour is visible in a .mdb database, using SQL to connect VB.Net with the Database. I have a seating plan set up as a grid in a form, using labels for the seats. The labels should turn red if the seat-code is shown in the booking database (the record is added when the seat is booked) Otherwise if it returns no records then the label stays green.
Private Sub CheckFriday()
Dim Friday(11, 20) As Control
'Friday Row A
Friday(1, 1) = FA1
Friday(1, 2) = FA2
Friday(1, 3) = FA3
Friday(1, 4) = FA4
Friday(1, 5) = FA5
Friday(1, 6) = FA6
Friday(1, 7) = FA7
Friday(1, 8) = FA8
Friday(1, 9) = FA9
Friday(1, 10) = FA10
Friday(1, 11) = FA11
Friday(1, 12) = FA12
Friday(1, 13) = FA13
Friday(1, 14) = FA14
'Friday Row B
Friday(2, 1) = FB1
Friday(2, 2) = FB2
Friday(2, 3) = FB3
Friday(2, 4) = FB4
Friday(2, 5) = FB5
Friday(2, 6) = FB6
Friday(2, 7) = FB7
Friday(2, 8) = FB8
Friday(2, 9) = FB9
Friday(2, 10) = FB10
Friday(2, 11) = FB11
Friday(2, 12) = FB12
Friday(2, 13) = FB13
Friday(2, 14) = FB14
Friday(2, 15) = FB15
Friday(2, 16) = FB16
'Friday Row C
Friday(3, 1) = FC1
Friday(3, 2) = FC2
Friday(3, 3) = FC3
Friday(3, 4) = FC4
Friday(3, 5) = FC5
Friday(3, 6) = FC6
Friday(3, 7) = FC7
Friday(3, 8) = FC8
Friday(3, 9) = FC9
Friday(3, 10) = FC10
Friday(3, 11) = FC11
Friday(3, 12) = FC12
Friday(3, 13) = FC13
Friday(3, 14) = FC14
Friday(3, 15) = FC15
Friday(3, 16) = FC16
Friday(3, 17) = FC17
'Friday Row D
Friday(4, 1) = FD1
Friday(4, 2) = FD2
Friday(4, 3) = FD3
Friday(4, 4) = FD4
Friday(4, 5) = FD5
Friday(4, 6) = FD6
Friday(4, 7) = FD7
Friday(4, 8) = FD8
Friday(4, 9) = FD9
Friday(4, 10) = FD10
Friday(4, 11) = FD11
Friday(4, 12) = FD12
Friday(4, 13) = FD13
Friday(4, 14) = FD14
Friday(4, 15) = FD15
Friday(4, 16) = FD16
Friday(4, 17) = FD17
Friday(4, 18) = FD18
Friday(4, 19) = FD19
'Friday Row E
Friday(5, 1) = FE1
Friday(5, 2) = FE2
Friday(5, 3) = FE3
Friday(5, 4) = FE4
Friday(5, 5) = FE5
Friday(5, 6) = FE6
Friday(5, 7) = FE7
Friday(5, 8) = FE8
Friday(5, 9) = FE9
Friday(5, 10) = FE10
Friday(5, 11) = FE11
Friday(5, 12) = FE12
Friday(5, 13) = FE13
Friday(5, 14) = FE14
Friday(5, 15) = FE15
Friday(5, 16) = FE16
Friday(5, 17) = FE17
Friday(5, 18) = FE18
Friday(5, 19) = FE19
Friday(5, 20) = FE20
'Friday Row F
Friday(6, 1) = FF1
Friday(6, 2) = FF2
Friday(6, 3) = FF3
Friday(6, 4) = FF4
Friday(6, 5) = FF5
Friday(6, 6) = FF6
Friday(6, 7) = FF7
Friday(6, 8) = FF8
Friday(6, 9) = FF9
Friday(6, 10) = FF10
Friday(6, 11) = FF11
Friday(6, 12) = FF12
Friday(6, 13) = FF13
Friday(6, 14) = FF14
Friday(6, 15) = FF15
Friday(6, 16) = FF16
Friday(6, 17) = FF17
Friday(6, 18) = FF18
Friday(6, 19) = FF19
Friday(6, 20) = FF20
'Friday Row G
Friday(7, 1) = FG1
Friday(7, 2) = FG2
Friday(7, 3) = FG3
Friday(7, 4) = FG4
Friday(7, 5) = FG5
Friday(7, 6) = FG6
Friday(7, 7) = FG7
Friday(7, 8) = FG8
Friday(7, 9) = FG9
Friday(7, 10) = FG10
Friday(7, 11) = FG11
Friday(7, 12) = FG12
Friday(7, 13) = FG13
Friday(7, 14) = FG14
Friday(7, 15) = FG15
Friday(7, 16) = FG16
Friday(7, 17) = FG17
Friday(7, 18) = FG18
Friday(7, 19) = FG19
'Friday Row H
Friday(8, 1) = FH1
Friday(8, 2) = FH2
Friday(8, 3) = FH3
Friday(8, 4) = FH4
Friday(8, 5) = FH5
Friday(8, 6) = FH6
Friday(8, 7) = FH7
Friday(8, 8) = FH8
Friday(8, 9) = FH9
Friday(8, 10) = FH10
Friday(8, 11) = FH11
Friday(8, 12) = FH12
Friday(8, 13) = FH13
Friday(8, 14) = FH14
Friday(8, 15) = FH15
Friday(8, 16) = FH16
Friday(8, 17) = FH17
Friday(8, 18) = FH18
Friday(8, 19) = FH19
'Friday Row J
Friday(9, 1) = FJ1
Friday(9, 2) = FJ2
Friday(9, 3) = FJ3
Friday(9, 4) = FJ4
Friday(9, 5) = FJ5
Friday(9, 6) = FJ6
Friday(9, 7) = FJ7
Friday(9, 8) = FJ8
Friday(9, 9) = FJ9
Friday(9, 10) = FJ10
Friday(9, 11) = FJ11
Friday(9, 12) = FJ12
Friday(9, 13) = FJ13
Friday(9, 14) = FJ14
Friday(9, 15) = FJ15
Friday(9, 16) = FJ16
Friday(9, 17) = FJ17
Friday(9, 18) = FJ18
Friday(9, 19) = FJ19
'Friday Row K
Friday(10, 1) = FK1
Friday(10, 2) = FK2
Friday(10, 3) = FK3
Friday(10, 4) = FK4
Friday(10, 5) = FK5
Friday(10, 6) = FK6
Friday(10, 7) = FK7
Friday(10, 8) = FK8
Friday(10, 9) = FK9
Friday(10, 10) = FK10
Friday(10, 11) = FK11
Friday(10, 12) = FK12
Friday(10, 13) = FK13
Friday(10, 14) = FK14
Friday(10, 15) = FK15
Friday(10, 16) = FK16
Friday(10, 17) = FK17
Friday(10, 18) = FK18
Friday(10, 19) = FK19
'Friday Row L
Friday(11, 1) = FL1
Friday(11, 2) = FL2
Friday(11, 3) = FL3
Friday(11, 4) = FL4
Friday(11, 5) = FL5
Friday(11, 6) = FL6
Friday(11, 7) = FL7
Friday(11, 8) = FL8
Friday(11, 9) = FL9
Friday(11, 10) = FL10
Friday(11, 11) = FL11
Friday(11, 12) = FL12
Friday(11, 13) = FL13
Friday(11, 14) = FL14
Friday(11, 15) = FL15
Dim A As Integer = 0
Dim x As Integer
Dim y As Integer
con.ConnectionString = dbProvider & dbSource
con.Open() 'opens the connection to the database
For y = 1 To 11
For x = 1 To 20
SQL = "SELECT * FROM Sales where SEAT_ID = '" & Friday(x, y) & "'"
da = New OleDb.OleDbDataAdapter(Sql, con) 'create a data adapter to store the filtered data using the SQL code
MsgBox(Sql)
da.Fill(ds, A)
A = A + 1
Dim recordCount As Integer
recordCount = ds.Tables(A).Rows.Count
MsgBox(recordCount)
If recordCount = 1 Then
Friday(x, y).BackColor = Color.Red
Else
Friday(x, y).BackColor = Color.Green
End If
Next x
Next y
End Sub
I get this error:
Error 1 Operator '&' is not defined for types 'String' and 'System.Windows.Forms.Control'. C:\Users\Connor Loughlin\Desktop\WindowsApplication1\WindowsApplication1\SeatingPlan.vb 238 23 SADS Booking System
Would be grateful for your suggestions or feedback, VB sure isn't my strongest subject.
Thank you for your help!
You're using the object instead of the text it represents. Try using .toString if your object supports that.
SQL = "SELECT * FROM Sales where SEAT_ID = '" & Friday(x, y).toString & "'"
I have a 5 by 5 matrix I want to populate and I would like to simplify this into for loops.
As I understand, I would need 2 for loops to complete this task?
I am still very new to VB hope you could understand
Dim x(4, 4) As Char
x(0, 0) = Mid(key, 1, 1)
x(0, 1) = Mid(key, 2, 1)
x(0, 2) = Mid(key, 3, 1)
x(0, 3) = Mid(key, 4, 1)
x(0, 4) = Mid(key, 5, 1)
x(1, 0) = Mid(key, 6, 1)
x(1, 1) = Mid(key, 7, 1)
....
x(4, 4) = Mid(key, 25, 1)
Try something like this:
Dim x As Integer
Dim y As Integer
Dim myMatrix(4, 4) As Char
For x = 0 To 4
For y = 0 To 4
myMatrix(x, y) = Mid(key, (x * 5) + y + 1, 1)
Next
Next