lundi 20 juin 2016

Filter and If condition in VBA Excel

I am trying to get data from user in VBA excel and match with a data set that has origin, Destination, Weight, Service level.

First, the user will enter origin, destination and weight. Based on the origin and destination, the program should check for weight slabs (compare entered weight with standard weight slabs) for that particular origin and destination and do a simple multiplication operation (entered weight * standard weight for the weight range) for different service level and returns the value to a worksheet.

The data table is in the link. Data table

A sample code that I wrote after watching few tutorials is below. I am not very sure what I am doing wrong. I am very new to programming and trying to do this to automate in my work. Any help is appreciated.

Sub Freight_Cost()

Dim Cost As Integer
Dim Weight As Integer

Weight = spreadsheets(Freight).Range("D4").Value
If Weight > 0 And Weight <= 45 Then
Cost.Value = Weight.Value * 10
Cost = spreadsheets(Freight).Range("F4").Value


ElseIf Weight > 45 And Weight <= 100 Then
Cost.Value = Weight.Value * 9
Cost = spreadsheets(Freight).Range("F4").Value

ElseIf Weight > 100 And Weight <= 300 Then
Cost.Value = Weight.Value * 8
Cost = spreadsheets(Freight).Range("F4").Value

Else: MsgBox "Invalid Weight"

End If

End Sub

Aucun commentaire:

Enregistrer un commentaire