VBA-可变范围的数据验证

时间:2018-11-27 05:02:44

标签: excel vba range

我正在尝试从另一个工作表中的变量范围设置数据验证列表。但是,我在定义范围的第一段代码时遇到了麻烦。这是我的代码:-

Private Sub Workload_Schedule_Conditional_Formatting()

Dim LastRowWS As Long, LastRowPS As Long, rng As Range, ProjectRange As Range

    LastRowPS = Worksheets("Project_Summary").Range("B" & Rows.Count).End(xlUp).Row
    Set ProjectRange = Worksheets("Project_Summary").Range(Cells(2, 1), Cells(LastRowPS, 2))
    LastRowWS = Worksheets("Workload_Schedule").Range("A" & Rows.Count).End(xlUp).Row
    Set rng = Worksheets("Workload_Schedule").Range(Cells(4, 3), Cells(LastRowWS, 7))

'Other code for validation list.

End Sub

Set rng行上出现错误。我不知道为什么会引发错误,但是Set ProjectRange行却没有(它基本上是完全相同的代码,但是在不同的工作表中)。

1 个答案:

答案 0 :(得分:0)

具有可读性

完整

import csv
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
map = Basemap(projection='stere',llcrnrlat=21.5, urcrnrlat=26, llcrnrlon=119.5, urcrnrlon=122.5, resolution='l', lat_0 = 22, lon_0=121)
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
map.drawcoastlines()
map.drawstates()
map.drawcountries()



class House:
    def __init__(self, num, x, y):
        self.num = num
        self.x = float(x)
        self.y = float(y)

lons = []
lats = []
color_lst = []
print(lons)

with open('/Users/yan/Downloads/tw-rental-data/2018Q3-raw-01.csv', newline='') as csvfile:
    rows = csv.DictReader(csvfile)

    for row in rows:
        if row['約略地點_x'] != '-' and row['約略地點_y'] != '-' and row['物件類型'] == '1':
            h = House(row['物件編號'], row['約略地點_x'], row['約略地點_y'])
            lons.append(h.y)
            lats.append(h.x)
            #print(row['物件編號'], row['約略地點_x'], row['約略地點_y'])
            #print(h.x)
            #map.scatter(h.x, h.y, 1, marker='o', color='k')
            if int(row['月租金']) < 4000:
                color_lst.append('lightcyan')
            elif 4000 <= int(row['月租金']) < 5000:
                color_lst.append('powderblue')
            elif 5000 <= int(row['月租金']) < 6000:
                color_lst.append('lightblue')
            elif 6000 <= int(row['月租金']) < 7000:
                color_lst.append('lightskyblue')
            elif 7000 <= int(row['月租金']) < 8000:
                color_lst.append('deepskyblue')
            elif 8000 <= int(row['月租金']) < 9000:
                color_lst.append('royalblue')
            elif 9000 <= int(row['月租金']) < 10000:
                color_lst.append('mediumblue')
            else:
                color_lst.append('midnightblue')
#print(lons)



x, y = map(lons, lats)
map.scatter(x, y, 1, marker='o',color=color_lst)


map.colorbar()
#plt.colorbar(x)

plt.show()

Sub Workload_Schedule_Conditional_Formatting()

    Dim LastRowWS As Long, LastRowPS As Long, rng As Range, _
            ProjectRange As Range

    With Worksheets("Project_Summary")
        LastRowPS = .Range("B" & .Rows.Count).End(xlUp).Row
        Set ProjectRange = .Range(Cells(2, 1), Cells(LastRowPS, 2))
    End With
    With Worksheets("Workload_Schedule")
        LastRowWS = .Range("A" & .Rows.Count).End(xlUp).Row
        Set rng = .Range(Cells(4, 3), Cells(LastRowWS, 7))
    End With

    ' Other code for validation list.

End Sub