Skip to content

Instantly share code, notes, and snippets.

@matthewhochler
Created October 28, 2016 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewhochler/8566106dfd0b37fcac0afb1849ddad66 to your computer and use it in GitHub Desktop.
Save matthewhochler/8566106dfd0b37fcac0afb1849ddad66 to your computer and use it in GitHub Desktop.
Public Function getRowNum(worksheetName, rowName, headerColumnNum) As Integer
Set worksheet = Worksheets(worksheetName)
rowNum = 0
lastRowNum = worksheet.Cells(worksheet.Rows.Count, headerColumnNum).End(xlup).Row
For rowLoopNum = 1 To lastRowNum
If worksheet.Cells(rowLoopNum, headerColumnNum).Value = rowName Then
rowNum = rowLoopNum
End If
Next rowLoopNum
getRowNum = rowNum
End Function
Public Function getColumnNum(worksheetName, columnName, headerRowNum) As Integer
Set worksheet = Worksheets(worksheetName)
columnNum = 0
lastColumnNum = worksheet.Cells(headerRowNum, worksheet.Columns.Count).End(xlToLeft).Column
For columnLoopNum = 1 To lastColumnNum
If worksheet.Cells(headerRowNum, columnLoopNum).Value = columnName Then
columnNum = columnLoopNum
End If
Next columnLoopNum
getColumnNum = columnNum
End Function
Sub FancyCalvin()
worksheetName = "Calvins Fancy Business" ' Name of your worksheet
headerRowNum = 1 ' Number of row your column names are in
headerColumnNum = 1 ' Number of column your row names are in
cellRowName = "Net Income" ' Name of row your desired cell is in
cellColumnName = "Actual Budget" ' Name of column your desired cell is in
' Get cell value from row and column names
Set worksheet = Worksheets(worksheetName)
cellRowNum = getRowNum(worksheetName, cellRowName, headerColumnNum)
cellColumnNum = getColumnNum(worksheetName, cellColumnName, headerRowNum)
cellValue = worksheet.Cells(cellRowNum, cellColumnNum).Value
' Do your thing here using "cellValue"
' For example, you can pop up an alert
MsgBox "Hey Calvin! Here's your cell value: " + Str(cellValue)
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment