What is the syntax to write to another workbook?

'to retrieve data
Workbooks("<workbook file name>").Worksheets("<sheet name>").Range("<your range>").Value
Workbooks("<workbook file name>").Worksheets("<sheet name>").Cells(<row number>, <column number>).Value

'to set data
Workbooks("<workbook file name>").Workbooks("Worksheets("<sheet name>").Range("<your range>") = <your value/data>
Workbooks("<workbook file name>").Worksheets("<sheet name>").Cells(<row number>, <column number>).Value = <your value/data>
'examples
Workbooks("example.xlsm").Worksheets("Example").Range("A1:A4") = "hello"
Workbooks("book3.xlsx").Worksheets("Example").Range("D1") = "world"
Workbooks("book3.xlsx").Worksheets("Example").Cells(2, 4).Value = "goodbye"
Advertisements

What is the syntax to load multiple images at once?

Dim cntrl as Control
For Each cntrl In <userformname>.Controls 'goes through all controls in form
   If TypeOf cntrl Is Image Then 'if control is image
      cntrl.Picture = LoadPicture(<picture's filepath here>)
   End If
Next 'moves onto next control
'example - updates the tiles (which are images) in a connect-3 game
'there are 90 image controls, all with naming convention: r<row>c<column>
'(ex) r01c01, r01c02, r01c03...r09c10
'there are 7 random colours a tile can be, the random number is stored in a sheet

Private Sub updateDisplay() 'call this procedure whenver display wanted to update
Dim cntrl As Control
Dim rr, cc As Integer 'for rows and columns
Dim strPath(7) As String

strPath(0) = "C:\Users\Documents\connect3\images\Yellow.jpg"
strPath(1) = "C:\Users\Documents\connect3\images\Green.jpg"
strPath(2) = "C:\Users\Documents\connect3\images\Pink.jpg"
strPath(3) = "C:\Users\Documents\connect3\images\White.jpg"
strPath(4) = "C:\Users\Documents\connect3\images\Red.jpg"
strPath(5) = "C:\Users\Documents\connect3\images\Cyan.jpg"
strPath(6) = "C:\Users\Documents\connect3\images\Orange.jpg"

For Each cntrl In frmConnect.Controls
   If TypeOf cntrl Is Image Then
      'cntrl.Name is in form r01c01, r01c02...r09c10
      rr = Int(Mid(cntrl.Name, 2, 2)) 'returns 3 if r03c04
      cc = Int(Mid(cntrl.Name, 5, 2)) 'returns 4 if r03c04
      cntrl.Picture = LoadPicture(strPath(Cells(rr, cc).Value))
      rr = rr + 1
      cc = cc + 1
   End If
Next
End Sub

retro

 

What is the syntax to select cells?

To select a single cell

Range("<Cell Value>").Select
(ex) Range("A1").Select
(ex) Range("C3").Select

 

To select a continuous range

Range("<Cell Value>").Select
(ex) Range("A1:B4").Select
(ex) Range("C3:F400").Select

 

To select multiple non-continuous ranges

Range("<Cell Value>, <Cell Value>").Select
(ex) Range("A1:E3, H2:H44").Select
(ex) Range("C3:D4, Y3:Y4").Select

 

To select down to the 1st empty cell

Range(Selection, Selection.End(xlDown)).Select

toselectdowntofirstemptycell