简体   繁体   中英

VBA code to create formula depending on cell value

I have a spreadsheet with values in cells A to G.

I need to read the data in Cell F, and depending on the data, formulate a hyperlink into the corresponding cell in column H, with the data from cell G (next to F) inserted within the hyperlink.

EG 1

Cell F contains : DPD

Cell g contains : 123456

Desire cell H to create hyperlink :

=HYPERLINK(" http://www.dpd.co.uk/tracking/quicktrack.do?search.consignmentNumber= "&CELLG*&"&search.searchType=16&search.javascriptValidated=0&appmode=guest")

EG 2

Cell F contains : TNT

Cell g contains : abcde

Desire cell H to create hyperlink :

=HYPERLINK(" http://www.tnt.com/express/en_gb/site/searchresults.html?q= CELLG ")

I have many more other values in cell F which rely on a different hyperlink formula, but I should be able to repeat and alter the code to cater for those values.

UPDATED:

This is my current attempt at applying the formula.

  Range("I1").Select
ActiveCell.FormulaR1C1 = _
    "=IF(RC[-2]=""dpd"",HYPERLINK(""http://www.dpd.co.uk/tracking/quicktrack.do?search.consignmentNumber=""&RC[-1]&""&search.searchType=16&search.javascriptValidated=0&appmode=guest""))"
Range("I1").Select
Selection.AutoFill Destination:=Range("i1:i" & Range("A" & Rows.Count).End(xlUp).Row), Type:=xlFillCopy
 Range("I2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

Range("A1:I1").Select
ActiveSheet.Range("A1:i1").AutoFilter Field:=9, Criteria1:="FALSE"
   Range("I2").Select
ActiveCell.FormulaR1C1 = _
    "=IF(R[-1]C[-2]=""City Link"",HYPERLINK(""http://www.packagetrackr.com/track/citylink/""&R[-1]C[-1]&""""))"
  Range("I2").Select
Selection.AutoFill Destination:=Range("i2:i" & Range("A" & Rows.Count).End(xlUp).Row), Type:=xlFillCopy

Range("A1:I1").Select
ActiveSheet.Range("A1:i1").AutoFilter Field:=9, Criteria1:="FALSE"
Range("i2").Select
ActiveCell.FormulaR1C1 = _
    "=IF(R[-1]C[-2]=""hermes"",HYPERLINK(""http://www.hermes-europe.co.uk/tracker.html?trackingNumber=""&R[-1]C[-1]&""&Postcode=""&R[-1]C[-4]&R[-1]C[-3]&""""))"
  Range("I2").Select
Selection.AutoFill Destination:=Range("i2:i" & Range("A" & Rows.Count).End(xlUp).Row), Type:=xlFillCopy

If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
Range("I2").Select
Selection.Delete Shift:=xlUp
Range("I1").Select

I cannot figure out how to autofill on a filtered list.

Maybe I need to try the loop through method?

FAILED FORMULA:

=IF(G2="hermes",HYPERLINK("www.hermes-europe.co.uk/tracker.html?trackingNumber="&H2&"&Postcode="&e2&f2&"),IF(G2="Parcelforce",HYPERLINK("www.parcelforce.com/track-trace?trackNumber="&h2&"),IF(G2="Royal Mail",HYPERLINK("www.royalmail.com/track-trace?track=track&trackNumber="&h2&"&gear=track&imageRootPath=&loc=en_GB&default=default&emt=emt"),IF(G2="Yodel",HYPERLINK("www.myyodel.co.uk/tracking?parcel_id="&h2&"&postcode="&e2&"%23"&f2&"),IF(G2="4 Square",HYPERLINK("login.smartconsign.co.uk/trackingcust.aspx?partnerid=EAFSQ001&jobno="&h2&"),IF(G2="Deutsche Post",HYPERLINK("nolp.dhl.de/nextt-online-public/set_identcodes.do?lang=en&idc="&h2&"&rfn=&extendedSearch=true"),IF(g2="No Tracking","&h2&")"""))))

You have to use the relevant cell's address rather than CELLG . See this example.

This goes in cell H1

To do a comparision in an Excel formula you can use the IF formula

Example

=If(Condition,Do if Cond is true, Do if Cond is False)

so

=IF(F2="TNT",Do Something if equal to TNT,Do Something if not equal to TNT))

or

=IF(F2="DPD",Do Something if equal to DPD,Do Something if not equal to DPD))

Combining the above two will give

=IF(F2="TNT",Do Something if equal to TNT,IF(F2="DPD",Do Something if equal to DPD,Do Something if not equal to DPD)))

If TNT then use this website

http://www.tnt.com/express/en_gb/site/searchresults.html?q=

If DPD then use this website

http://www.dpd.co.uk/tracking/quicktrack.do?search.consignmentNumber=

All we have to do now is append it to the above formula

=IF(F1="TNT",HYPERLINK("http://www."&F1&".com/express/en_gb/site/searchresults.html?q="& G1& "&search.searchType=16&search.javascriptValidated=0&appmode=guest"),IF(F1="DPD",HYPERLINK("http://www."&F1&".co.uk/tracking/quicktrack.do?search.consignmentNumber="& G1& "&search.searchType=16&search.javascriptValidated=0&appmode=guest"),""))

Screenshot

在此处输入图片说明

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