简体   繁体   中英

How to assess Cell from another worksheet in Excel VBA code

am trying to check if a specific cell (G12) from a Worksheet ("Enter_information") is blank ( am using an excel worksheet instead of a user form since i need to include a complex table for user to input data ). my current worksheet is ("DQA_Database") and when i instruct code to assess the cell (G12), i keep getting an 1004 error. the code is as below:

Private Sub cmdAdd_Click()

'Adding info to database
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("DQA_Database")

'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'check for missing facility info (if cell is blank)
If Trim(Me.Range(G12).Value) = "" Then
  Me.Range(G12).SetFocus
  MsgBox "Please select a district"
  Exit Sub
End If

please, help a brother.

Without rewriting your code, here's an example of pointing to a cell on another sheet, reading its value, and setting focus to it (well, selecting it actually)

Private Sub testButton_Click()

    Dim s As Worksheet
    Dim r As Range
    Set s = ActiveWorkbook.Sheets("Enter_information")
    Set r = s.Range("G12")
    Debug.Print r.Value
    s.Select
    r.Select

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM