I am trying to generate map from GCmap.com using the VBA code.
I do have a long list of airport pairs in one column. The format of the hyperlink that will generate the map will be:
"http://www.gcmap.com/map?P="&(calue in .cell(1.51))&"%0d%0a"&(calue in .cell(2.51)& so on ...
' the %0d%0a - is a separator that needs to be between values
the problem is that this list is a quite long and just doing simple loop will cause enormous length of hyperlink (sometimes too long to generate the map) so it must be something like
if valueOfFirstCell = ValueOfCellBellow then
' skip to next one
I've tried this (probably worst ever written code, but please note that I am only beginner in VBA :)
Sub hyperlinkgenerator()
Dim separator As String ' declared the 'separator' value
separator = "%0d%0a"
Dim datarow, irow As Integer
Dim myval as Range
With Sheets("Direct flights")
Set myval = .Cells(datarow, 51)
Do While myval <> "" ' do until last empty row
Dim Value1 As String
Value1 = Sheets("Direct flights").Cells(datarow, 51) ' declare value of the first cell
Dim Value2 As String
Value2 = Sheets("Direct flights").Cells(datarow + 1, 51) ' declare value of cell bellow
If Value1 = Value2 Then
datarow = datarow + 1
Else: 'enter Value1 in hyperlink that is followed by & separator
'also the hyperlink must start with: http://www.gcmap.com/map?P=
' and end with %0d%0a&MS=wls&MR=540&MX=720x360&PM=*
End If
datarow = datarow + 1
Loop
End With
End Sub
The final link will look like:
http://www.gcmap.com/map?P=LGW-AMS%0d%0aAMS-LHR%0d%0aLCY-AMS%0d%0aLUX-AMS%0d%0aBRE-AMS%0d%0aCWL-AMS%0d%0aNUE-AMS%0d%0aAMS-JFK%0d%0a&MS=wls&MR=540&MX=720x360&PM=*
I don't have a clue how to keep loop adding new and new text into Hyperlink. Also hyperlink should be generated by and opened once user will click on button (that is easy to do).
Many thanks in advance!
Give this a try:
Sub hyperlinkgenerator()
Dim thisVal As String
Dim nextVal As String
Dim currentRow As Long
Dim hyperlink As String
currentRow = 1 ' or whatever the first row is
hyperlink = ""
With Sheets("Direct flights")
Do While True
thisVal = .Cells(currentRow, 1).Value
nextVal = .Cells(currentRow + 1, 1).Value
If thisVal = "" Then
Exit Do
ElseIf thisVal <> nextVal Then
hyperlink = hyperlink & thisVal & "%0d%0a"
End If
currentRow = currentRow + 1
Loop
End With
hyperlink = "http://www.gcmap.com/map?P=" & hyperlink & "&MS=wls&MR=540&MX=720x360&PM="
MsgBox hyperlink
End Sub
Related
I'm trying to look for values to create a final ticket number for a ticket reconciliation process. This is what should happen:
subroutine looks for a value in cell "Gx"
if it finds a value
pass value to function to strip out letters, convert to a number, pass back to subroutine to place in
cell "Ax"
if there is no value
pass value of "Cx" to function etc.
This loops through the number cells I have in my worksheet based on the number of rows filled in a separate column.
The function works fine by itself in the worksheet, but when I pass it a value from the subroutine column A fills up with the number of the row ie. A37=37, A8=8. I don't think I'm passing the argument correctly to the function, but I'm not certain. Here's the code for the subroutine and the function:
Sub final_ticket_number()
Dim rw As Integer
Dim i As Integer
'header label
Range("A1").Value = "Final Ticket #"
'set number of rows for loop
With Worksheets(1)
rw = .Range("B2").End(xlDown).Row
End With
'check col G for empty, use col C as backup
For i = 2 To rw
If Not IsEmpty(Cells(i, "G")) Then
'strip out letters in col G, place in col A
Cells(i, "A").Value = getNumeric("G" & i)
Else
'strip out letters in col C, place in col A
Cells(i, "A").Value = getNumeric("C" & i)
End If
Next i
End Sub
Function getNumeric(cellRef As String) As Long 'remove letters from ticket numbers
Dim stringLength As Integer
Dim i As Byte
Dim Result As String
stringLength = Len(cellRef)
'loops through each character in a cell to evaluate if number or not
For i = 1 To stringLength
If IsNumeric(Mid(cellRef, i, 1)) Then
Result = Result & Mid(cellRef, i, 1)
End If
Next i
'convert remaining characters to number
getNumeric = CLng(Result)
End Function
What am I missing?
As I understand it, the only thing that is wrong is your Len (cellRef), here you are only passing the range and not his value. See how I did it, I had to specify the spreadsheet, do the same that will work.
Use debug.print to see the outputs of the variables. Write in the code "debug.print XvariableX" and in the immediate check (Ctrl + G) you see the value assigned to the variable. good luck.
Sub final_ticket_number()
Dim rw As Integer
Dim i As Integer
Range("A1").Value = "Final Ticket #"
With Worksheets(1)
rw = .Range("B2").End(xlDown).Row
End With
For i = 2 To rw
If Not IsEmpty(Cells(i, "G")) Then
Cells(i, "A").Value = getNumeric("G" & i)
Else
Cells(i, "A").Value = getNumeric("C" & i)
End If
Next i
End Sub
Function getNumeric(cellRef As String) As Long 'remove letters from ticket numbers
Dim stringLength As Integer
Dim i As Byte
Dim Result As String
Dim Wrs As String
Wrk = ActiveWorkbook.Name
Workbooks(Wrk).Activate
Wrs = ActiveSheet.Name
stringLength = Len(Workbooks(Wrk).Worksheets(Wrs).Range(cellRef))
For i = 1 To stringLength
If IsNumeric(Mid(Workbooks(Wrk).Worksheets(Wrs).Range(cellRef), i, 1)) Then
Result = Result & Mid(Workbooks(Wrk).Worksheets(Wrs).Range(cellRef), i, 1)
End If
Next i
getNumeric = CLng(Result)
End Function
I have an excel table that looks like this:
Row Name
1 uniqueName001_vid1.mpg
2 uniqueName001.mpg
3 uniqueName002_vid1.mpg
4 uniqueName002_vid2.mpg
5 uniqueName002.mpg
I am trying to figure out how to identify and flag(give a unique ID) sets within the table that contain the same uniqueName. For instance Row's 1 and 2 would be one set and Row's 3, 4, and 5 would be another set.
My ideal result is this:
Row Name UID
1 uniqueName001_vid1.mpg SET1
2 uniqueName001.mpg SET1
3 uniqueName002_vid1.mpg SET2
4 uniqueName002_vid2.mpg SET2
5 uniqueName002.mpg SET2
I can run a SQL query in excel if that is better option than excel formula's too.
Any suggestions are greatly appreciated!
If all starts with uniqueNameXXX than it is easy
Row Name UniqueName Unique# UID
1 uniqueName001_vid1.mpg =LEFT(F4;13) =IF(G3<>G4;H3+1;H3) ="UID"&H4
If not, than you should define how to get uniqueName
You can use VBA for that task.
I made a little tool for you. Take care of the editable part
under the declarations.
This tool listens on numbers - means, I expect your pattern to be always the same as you wrote in your question.
Tell me if this helped:
Sub ExtractIdFromString()
Dim strYourColumn As String
Dim intYourStartRow As Integer
Dim intYourLengthOfId As Integer
Dim strYourSetColumn As String
Dim strYourPrefix As String
Dim strString As String
Dim intStringLength As Integer
Dim intStringDigitPosition As Integer
Dim intParserPosition As Integer
Dim strParser As String
Dim i As Integer
Dim strUniqueString As String
Dim rngCell As Range
Dim rngSetCell As Range
Dim strIndex As String
Dim lngCounter As Long
''''editable values''''
strYourColumn = "B" 'Your name column, must be alphabethical
intYourStartRow = 1 'Startrow of your block, must not be 0
intYourLengthOfId = 3 'The amount of digits in your ID, must be > 1
strYourSetColumn = "C" 'The column, where the ID will be inserted, must be numerical (use A = 1, Z = 26)
strYourPrefix = "SET" 'Prefix of your set's ID
''''end of editable values''''
'Set the format of the ID column to text
Range(strYourColumn & ":" & strYourColumn).NumberFormat = "#"
'traverse through the names column
For Each rngCell In Range(strYourColumn & ":" & strYourColumn)
'initialize / reset parser
intParserPosition = 1
'get the actual string to value
strString = rngCell.Value
'End loop on empty cell
If strString = "" Then
GoTo massRename
End If
'get the string's length
intStringLength = Len(strString)
'parse through the string
For intStringDigitPosition = 1 To intStringLength Step 1
'end loop if the string is parsed without a result
If intParserPosition > intStringLength Then
Exit For
End If
'get single digit of the string
strParser = Mid(strString, intParserPosition, 1)
'listen on numbers
If IsNumeric(strParser) Then
'traverse through the expected ID slots
For i = intParserPosition To intParserPosition + intYourLengthOfId - 1 Step 1
'listen for non numerical chars in the expected ID
If Not IsNumeric(Mid(strString, i, 1)) Then
'allow your titles to include numbers
GoTo SkipSingleNumerics
End If
Next
'get the unique prototype of the string
strUniqueString = Mid(strString, 1, intParserPosition + intYourLengthOfId - 1)
'write the unique name in a specified column
Range(strYourSetColumn & rngCell.Row).Value = strUniqueString
End If
'Skip numbers in the string, that dont dont match the ID pattern (optional)
SkipSingleNumerics:
'traverse trough the word
intParserPosition = intParserPosition + 1
Next
Next
'Rename and index equal values
massRename:
lngCounter = 1
'traverse through the set list
For Each rngSetCell In Range(strYourSetColumn & ":" & strYourSetColumn)
'end condition
If rngSetCell.Value = "" Then
Exit For
End If
'store value in variable to save it from overwriting
strIndex = rngSetCell.Value
'start another traversal instance
For Each rngCell In Range(strYourSetColumn & ":" & strYourSetColumn)
'end condition
If rngCell.Value = "" Then
Exit For
End If
'listen if both instances match
If strIndex = rngCell.Value Then
'rename the value
rngCell.Value = strYourPrefix & lngCounter
End If
Next
'increase unique counter
lngCounter = lngCounter + 1
Next
End Sub
tested in Excel 2010
I'm currently struggling with the following problem
I'm trying to implement an input box where a user can input the character of the column.
After that, i dont know how to convert this into a number to use it in the Worksheet.Cells Method
For example: The User inputs "B", so the program saves it in a variable named x and converts it into a number so it can be used as Worksheet.Cells(Row, X)
Is there any method or does someone has an idea how to do this?
Cells() is your friend.
Don't overthink this.
Cells(1, 1) = "jello" ' changes cell A1
Cells(2, "AZ") = "too much jello" ' changes cell AZ2
The second argument of Cells() can be either a number or the alpha column header.
B is the second column, so you can use the expression (based on ASCII):
Sub main()
Dim s As String
s = "AB"
example s
End Sub
Sub example(s As String)
Dim colNum As Integer
Dim i As Integer
i = 1: colNum = 0
While (Mid(s, i, 1) <> "")
colNum = colNum * 26 + (Asc(UCase(Mid(s, i, 1))) - Asc("A") + 1)
i = i + 1
Wend
MsgBox colNum
End Sub
Function getColNum(colLetter As String) As Long
On Error Resume Next 'will return 0 if letter > last col
getColNum = Range(colLetter & "1").Column
End Function
I am currently attempting to take user input and split each bit of input using a comma. Currently I am trying to use -
includer = CStr(InputBox("Do you have any inclusions? Separate words with commas"))
inclusion = Split(includer, ",", , vbTextCompare)
with no luck.
It keeps throwing a 'Type Mismatch' error.
The full code I am attempting to use, for anyone that is interested, is-
Option Compare Text
Public Sub Textchecker()
'
' Textchecker
'
' Keyboard Shortcut: Ctrl+h
'
Dim Continue As Long
Dim findWhat As String
Dim LastLine As Long
Dim toCopy As Boolean
Dim cell As Range
Dim item As Long
Dim j As Long
Dim x As Long
Dim sheetIndex As Long
Dim inclusion() As String
sheetIndex = 2
Continue = vbYes
Do While Continue = vbYes
findWhat = CStr(InputBox("What word would you like to search for today?"))
includer = CStr(InputBox("Do you have any inclusions? Separate words with commas"))
inclusion = Split(includer, ",", , vbTextCompare)
LastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For item = 1 To LastLine
For Each cell In Range("BY1").Offset(item - 1, 0)
If InStr(cell.Text, findWhat) <> 0 And InStr(cell.Text, inclusion(x)) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Sheets(sheetIndex).Name = UCase(findWhat) + "+" + LCase(inclusion(x))
Rows(item).Copy Destination:=Sheets(sheetIndex).Rows(j)
j = j + 1
End If
toCopy = False
Next item
sheetIndex = sheetIndex + 1
Continue = MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion)
Loop
End Sub
Change
Dim inclusion As String
to
Dim inclusion() As String 'recommended in my opinion
or
Dim inclusion As variant
Edit
I see your use of inclusion later in the code. It looks like you need to iterate over your new array.
dim x as long 'put this at the top, rename as you'd like.
for x = lbound(inclusion) to ubound(inclusion)
'do stuff with inclusion(x), i other code here before but I'm not sure that it was right anymore.
next
Edit 2
For item = 1 To LastLine
If UBound(insulation) >= 0 Then
'write procedure for when inclusion are present
Else
'write 2nd procedure for when inclusions are not present
End If
Next item
when we are going to do a loop in the rows, we can use code like the following:
i = 1
Do
Range("E" & i & ":D" & i).Select
i = i + 1
Loop Until i > 10
but what if we want to do a loop on a column?
Can we use the same method as above?
while the columns in Excel is a complex such as A, B, C, ..., Y, Z, AA, AB, AC, ..., etc.
problems will arise between loop from the "Z" to the "AA".
how we do looping alphabet column from "A" to "Z" and then continued into "AA", "AB" and so on
is there anything that can help?
Yes, let's use Select as an example
sample code: Columns("A").select
How to loop through Columns:
Method 1: (You can use index to replace the Excel Address)
For i = 1 to 100
Columns(i).Select
next i
Method 2: (Using the address)
For i = 1 To 100
Columns(Columns(i).Address).Select
Next i
EDIT:
Strip the Column for OP
columnString = Replace(Split(Columns(27).Address, ":")(0), "$", "")
e.g. you want to get the 27th Column --> AA, you can get it this way
Another method to try out.
Also select could be replaced when you set the initial column into a Range object. Performance wise it helps.
Dim rng as Range
Set rng = WorkSheets(1).Range("A1") '-- you may change the sheet name according to yours.
'-- here is your loop
i = 1
Do
'-- do something: e.g. show the address of the column that you are currently in
Msgbox rng.offset(0,i).Address
i = i + 1
Loop Until i > 10
** Two methods to get the column name using column number**
Split()
code
colName = Split(Range.Offset(0,i).Address, "$")(1)
String manipulation:
code
Function myColName(colNum as Long) as String
myColName = Left(Range(0, colNum).Address(False, False), _
1 - (colNum > 10))
End Function
If you want to stick with the same sort of loop then this will work:
Option Explicit
Sub selectColumns()
Dim topSelection As Integer
Dim endSelection As Integer
topSelection = 2
endSelection = 10
Dim columnSelected As Integer
columnSelected = 1
Do
With Excel.ThisWorkbook.ActiveSheet
.Range(.Cells(columnSelected, columnSelected), .Cells(endSelection, columnSelected)).Select
End With
columnSelected = columnSelected + 1
Loop Until columnSelected > 10
End Sub
EDIT
If in reality you just want to loop through every cell in an area of the spreadsheet then use something like this:
Sub loopThroughCells()
'=============
'this is the starting point
Dim rwMin As Integer
Dim colMin As Integer
rwMin = 2
colMin = 2
'=============
'=============
'this is the ending point
Dim rwMax As Integer
Dim colMax As Integer
rwMax = 10
colMax = 5
'=============
'=============
'iterator
Dim rwIndex As Integer
Dim colIndex As Integer
'=============
For rwIndex = rwMin To rwMax
For colIndex = colMin To colMax
Cells(rwIndex, colIndex).Select
Next colIndex
Next rwIndex
End Sub
Just use the Cells function and loop thru columns.
Cells(Row,Column)